Files
common/packages/util/docs/bn.md
T
Jaco Greeff f79d7fe68e bnToU8a supports endian (#87)
* bnToU8a supports endian

* Update with new bn.js flow
2018-02-13 14:30:10 +01:00

1.8 KiB

bn

Utility methods to convert to and from BN objects

  • bnFromHex Creates a BN.js bignumber object from a hex string.
  • bnToBn Creates a BN value from a BN.js bignumber or number input.
  • bnToHex Creates a hex value from a BN.js bignumber object.
  • bnToU8a Creates a Uint8Array object from a BN.

bnFromHex

Creates a BN.js bignumber object from a hex string. (alias of hexToBn)

bnFromHex (value?: string): BN

bnToBn

Creates a BN value from a BN.js bignumber or number input.

bnToBn (value?: BN : number): BN

null inputs returns a 0x0 result, BN values returns the value, numnbers returns a BN representation.

import BN from 'bn.js';
import { bnToBn } from '@polkadot/util';

bnToBn(0x1234); // => BN(0x1234)
bnToBn(new BN(0x1234)); // => BN(0x1234)

bnToHex

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

bnToHex (value?: BN, bitLength: number = -1): string

null inputs returns a 0x result, BN values return the actual value as a 0x prefixed hex value. Anything that is not a BN object throws an error. With bitLength set, it fixes the number to the specified length.

import BN from 'bn.js';
import { bnToHex } from '@polkadot/util';

bnToHex(new BN(0x123456)); // => '0x123456'

bnToU8a

Creates a Uint8Array object from a BN.

bnToU8a (value?: BN | number, bitLength: number = -1, isLE: boolean = false): Uint8Array

null/undefined/NaN inputs returns an empty Uint8Array result. BN input values return the actual bytes value converted to a Uint8Array. Optionally convert using little-endian format if isLE is set.

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

bnToU8a(new BN(0x1234)); // => [0x12, 0x34]