Files
common/docs/hex.md
T
Jaco Greeff d02c05ba49 Alias to <-> from (#31)
* bn <-> hex aliasses

* Bump minor

* Split tests into singular files

* to <-> from alias

* Add Codeclimate badge
2017-11-30 20:16:17 +01:00

3.3 KiB

hex

Internal utilities to create and test for hex values

  • hexAddPrefix Adds the 0x prefix to string values.
  • hexFromBn Creates a hex value from a BN.js bignumber object. [bn.md#bntohex](alias bnToHex)
  • hexFromBn Creates a hex value from a Buffer object. [buffer.md#buffertohex](alias bufferToHex)
  • hexFromNumber Creates a hex value from a number. [number.md#numbertohex](alias numberToHex)
  • hexHasPrefix Tests for the existence of a 0x prefix.
  • hexStripPrefix Strips any leading 0x prefix.
  • 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

hexFromBn

Creates a hex value from a BN.js bignumber object. [bn.md#bntohex](alias bnToHex)

hexFromBn (value?: BN): string

hexFromBn

Creates a hex value from a Buffer object. [buffer.md#buffertohex](alias bufferToHex)

hexFromBuffer (value?: Buffer): string

hexFromNumber

Creates a hex value from a number. [number.md#numbertohex](alias 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';

console.log('BN object', hexToBn('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';

console.log('Buffer object', hexToBuffer('0x123480001f'));

hexToNumber

Creates a Number value from a Buffer object.

hexToNumber (value?: Hex): number

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

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

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