@premix/buffer
v0.2.0
Published
Handle ArrayBuffer easily
Downloads
3
Maintainers
Readme
This package is used to help people to handle buffer easily.
Between ArrayBuffer and Buffer
ArrayBuffer object is used to represent a generic, fixed-length raw binary data buffer. Buffer object in node.js is equivalent to Uint8Array (A kind of TypedArray)source, not ArrayBuffer object.
A TypedArray object describes an array-like view of an underlying binary data buffer.
With TypedArray available, the Buffer class implements the Uint8Array API in a manner that is more optimized and suitable for Node.js.
Relationship with binary
The Number type is a double-precision 64-bit binary format IEEE 754 value (numbers between -(253 -1) and 253 -1).
The String type is a set of "elements" of 16-bit unsigned integer values
Byte order
If control over byte order is needed, you may need to use DataView and take care of endian, endianness, byte-order.
Usage
- convert a string data type to an arrayBuffer
import { stringToBuffer } from '@premix/buffer'
stringToBuffer('a') // return a ArrayBuffer<byteLength=2>
- convert an arrayBuffer to a string
import { bufferToString } from '@premix/buffer'
const view = new Uint16Array(new ArrayBuffer(2))
view[0] = 'a'.charCodeAt(0)
bufferToString(view.buffer) // return a string 'a'
Further readings
- MDN - JavaScript data types and data structures
- MDN - ArrayBuffer
- Nodejd.org - Buffer
- MDN - TypedArray
- ECMA 262 - TypedArray
License
MIT © Bowen Liu