UNPKG

653 BJavaScriptView Raw
1import { hexToU8a } from '../hex/toU8a.js';
2import { numberToHex } from './toHex.js';
3/**
4 * @name numberToU8a
5 * @summary Creates a Uint8Array object from a number.
6 * @description
7 * `null`/`undefined`/`NaN` inputs returns an empty `Uint8Array` result. `number` input values return the actual bytes value converted to a `Uint8Array`. With `bitLength`, it converts the value to the equivalent size.
8 * @example
9 * <BR>
10 *
11 * ```javascript
12 * import { numberToU8a } from '@polkadot/util';
13 *
14 * numberToU8a(0x1234); // => [0x12, 0x34]
15 * ```
16 */
17export function numberToU8a(value, bitLength = -1) {
18 return hexToU8a(numberToHex(value, bitLength));
19}