UNPKG

949 BJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.u8aToHex = void 0;
4/**
5 * @name u8aToHex
6 * @summary Creates a hex string from a Uint8Array object.
7 * @description
8 * `UInt8Array` input values return the actual hex string. `null` or `undefined` values returns an `0x` string.
9 * @example
10 * <BR>
11 *
12 * ```javascript
13 * import { u8aToHex } from '@polkadot/util';
14 *
15 * u8aToHex(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0xf])); // 0x68656c0f
16 * ```
17 */
18function u8aToHex(value, bitLength = -1, isPrefixed = true) {
19 const length = Math.ceil(bitLength / 8);
20 return `${isPrefixed ? '0x' : ''}${!value?.length
21 ? ''
22 : (bitLength > 0 && value.length > length)
23 ? `${Buffer.from(value.subarray(0, length / 2)).toString('hex')}…${Buffer.from(value.subarray(value.length - length / 2)).toString('hex')}`
24 : Buffer.from(value).toString('hex')}`;
25}
26exports.u8aToHex = u8aToHex;