UNPKG

898 BJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.numberToHex = void 0;
4const fixLength_js_1 = require("../hex/fixLength.js");
5/**
6 * @name numberToHex
7 * @summary Creates a hex value from a number.
8 * @description
9 * `null`/`undefined`/`NaN` inputs returns an empty `0x` result. `number` input values return the actual bytes value converted to a `hex`. With `bitLength` set, it converts the number to the equivalent size.
10 * @example
11 * <BR>
12 *
13 * ```javascript
14 * import { numberToHex } from '@polkadot/util';
15 *
16 * numberToHex(0x1234); // => '0x1234'
17 * numberToHex(0x1234, 32); // => 0x00001234
18 * ```
19 */
20function numberToHex(value, bitLength = -1) {
21 const hex = (!value || Number.isNaN(value) ? 0 : value).toString(16);
22 return (0, fixLength_js_1.hexFixLength)(hex.length % 2 ? `0${hex}` : hex, bitLength, true);
23}
24exports.numberToHex = numberToHex;