Files
common/docs/buffer.md
T
Jaco Greeff 01173aea60 Add logger for consistent info output (#14)
* Add logger as a util

* Add logger

* Update flow checks
2017-11-23 19:37:02 +01:00

40 lines
1.0 KiB
Markdown

# buffer
Utility methods to convert to and from `Buffer` objects
- [bufferFromHex](#bufferfromhex) Creates a Buffer object from a hex string.
- [bufferToHex](#buffertohex) Creates a hex value from a Buffer object.
## bufferFromHex
Creates a Buffer object from a hex string.
```js
bufferFromHex (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.
```js
import { bufferFromHex } from '@polkadot/util';
console.log('Buffer object', bufferFromHex('0x123480001f'));
```
## bufferToHex
Creates a hex value from a Buffer object.
```js
bufferToHex (value?: Buffer): string
```
`null` inputs returns a `0x` result, `Buffer` values return the actual value as a `0x` prefixed hex value. Anything that is not a `Buffer` object throws an error.
```js
import { bufferToHex } from '@polkadot/util';
console.log('Hex value', bufferToHex(Buffer.from([1, 2, 3]));
```