Files
common/packages/util/docs/u8a.md
T
Jaco Greeff b5c5734c7c Add Blake2s & XXHash utility functions (#66)
* 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
2018-01-25 14:13:43 +01:00

1.6 KiB

u8a

Utility methods to convert to and from Uint8Array objects

  • u8aFromBuffer Creates a Uint8Array value from a Buffer object.
  • u8aFromUtf8 Creates a Uint8Array object from a utf-8 string.
  • u8aToBuffer Creates a Buffer object from a hex string.
  • u8aToUtf8 Creates a utf-8 string from a Uint8Array object.

u8aFromBuffer

Creates a Uint8Array value from a Buffer object. (alias of bufferToU8a)

u8aFromBuffer (value?: Buffer): number

u8aFromUtf8

Creates a Uint8Array object from a utf-8 string.

u8aFromUtf8 (value?: string): UInt8Array

String input values return the actual encoded UInt8Array. null or undefined values returns an empty encoded array.

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

u8aFromUtf8('hello'); // [0x68, 0x65, 0x6c, 0x6c, 0x6f]

u8aToBuffer

Creates a Buffer object from a hex string.

u8aToBuffer (value?: UInt8Array): Buffer

null inputs returns an empty Buffer result. UInt8Array input values return the actual bytes value converted to a Buffer. Anything that is not a UInt8Array throws an error.

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

console.log('Buffer', u8aToBuffer('0x123480001f'));

u8aToUtf8

Creates a utf-8 string from a Uint8Array object.

u8aToUtf8 (value?: UInt8Array): string

UInt8Array input values return the actual decoded utf-8 string. null or undefined values returns an empty string.

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

u8aToUtf8(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f])); // hello