Files
common/packages/util/docs/hex.md
T

4.6 KiB

hex

Internal utilities to create and test for hex values

hexAddPrefix

Adds the 0x prefix to string values.

hexAddPrefix (value: ?string): string

Returns a 0x prefixed string from the input value. If the input is already prefixed, it is returned unchanged.

import { hexAddPrefix } from '@polkadot/util';

console.log('With prefix', hexAddPrefix('0a0b12')) // => 0x0a0b12

hexFixLength

Shifts a hex string to a specific bitLength

hexFixLength (value: string, bitLength: number = -1, withPadding: boolean = false): string

Returns a 0x prefixed string with the specified number of bits contained in the return value. (If bitLength is -1, length checking is not done). Values with more bits are trimmed to the specified length. Input values with less bits are returned as-is by default. When withPadding is set, shorter values are padded with 0.

import { hexFixLength } from '@polkadot/util';

console.log('fixed', hexFixLength('0x12', 16)) // => 0x12
console.log('fixed', hexFixLength('0x12', 16, true)) // => 0x0012
console.log('fixed', hexFixLength('0x0012', 8)) // => 0x12

hexFromBn

Creates a hex value from a BN.js bignumber object. (alias of bnToHex)

hexFromBn (value?: BN): string

hexFromBuffer

Creates a hex value from a Buffer object. (alias of bufferToHex)

hexFromBuffer (value?: Buffer): string

hexFromNumber

Creates a hex value from a number. (alias of numberToHex)

hexFromNumber (value?: number): string

hexHasPrefix

Tests for the existence of a 0x prefix.

hexHasPrefix (value: ?string): boolean

Checks for a valid hex input value and if the start matched 0x

import { hexHasPrefix } from '@polkadot/util';

console.log('has prefix', hexHasPrefix('0x1234')); // => true

hexStripPrefix

Strips any leading 0x prefix.

hexStripPrefix (value: ?string): string

Tests for the existence of a 0x prefix, and returns the value without the prefix. Un-prefixed values are returned as-is.

import { hexStripPrefix } from '@polkadot/util';

console.log('stripped', hexStripPrefix('0x1234')); // => 1234

hexToBn

Creates a BN.js bignumber object from a hex string.

hexToBn (value?: string): BN

null inputs returns a BN(0) result. Hex input values return the actual value converted to a BN. Anything that is not a hex string (including the 0x prefix) throws an error.

import { hexToBn } from '@polkadot/util';

hexToBn('0x123480001f'); // => BN(0x123480001f)

hexToBuffer

Creates a Buffer object from a hex string.

hexToBuffer (value?: string): Buffer

null inputs returns an empty Buffer result. Hex input values return the actual bytes value converted to a Buffer. Anything that is not a hex string (including the 0x prefix) throws an error.

import { hexToBuffer } from '@polkadot/util';

hexToBuffer('0x123480001f'); // Buffer([0x12, 0x34, 0x80, 0x00, 0x1f])

hexToNumber

Creates a Number value from a Buffer object.

hexToNumber (value?: string): number

null inputs returns an NaN result, hex values return the actual value as a Number.

import { hexToNumber } from '@polkadot/util';

hexToNumber('0x1234'); // => 0x1234

hexToU8a

Creates a Buffer object from a hex string.

hexToU8a (value?: string, bitLength: number = -1): Uint8Array

null inputs returns an empty Uint8Array result. Hex input values return the actual bytes value converted to a Uint8Array. Anything that is not a hex string (including the 0x prefix) throws an error.

import { hexToU8a } from '@polkadot/util';

hexToU8a('0x80001f'); // Uint8Array([0x80, 0x00, 0x1f])
hexToU8a('0x80001f', 32); // Uint8Array([0x00, 0x80, 0x00, 0x1f])