* Initial split of util & util-crypto * Add initial XXHash version * Blake2s wit tests & documentation * Add sha512 (from NaCl) * Add randomBytes (from NaCl) * Flow fixes * Update documentation * Update latest @polkadot/dev
4.0 KiB
hex
Internal utilities to create and test for hex values
- hexAddPrefix Adds the
0xprefix to string values. - hexFixLength Shifts a hex string to a specific bitLength
- hexFromBn Creates a hex value from a BN.js bignumber object.
- hexFromBuffer Creates a hex value from a Buffer object.
- hexFromNumber Creates a hex value from a number.
- hexHasPrefix Tests for the existence of a
0xprefix. - hexStripPrefix Strips any leading
0xprefix. - hexToBn Creates a BN.js bignumber object from a hex string.
- hexToBuffer Creates a Buffer object from a hex string.
- hexToNumber Creates a Number value from a Buffer object.
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 val;ues 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