Skip to content

Typed Arrays

Sometimes binary is the format that we want to work with directly. Typed array schemas allow that data to be nested in plain objects or arrays.

const Image = bin.object({
size: bin.tupleOf([bin.u32, bin.u32]),
bitmap: bin.u8Array(256 * 256),
});
// {
// size: [number, number];
// bitmap: Uint8Array;
// }
const image = Image.read(...);
image.size // [number, number]
image.bitmap // Uint8Array

Below is the list of available typed array schemas.

Schema constructorEncoded asJavaScript value
bin.u8Arrayconsecutive 8-bit unsigned integersUint8Array
bin.u8ClampedArrayconsecutive 8-bit unsigned integersUint8ClampedArray
bin.u16Arrayconsecutive 16-bit unsigned integersUint16Array
bin.u32Arrayconsecutive 32-bit unsigned integersUint32Array
bin.i8Arrayconsecutive 8-bit signed integersInt8Array
bin.i16Arrayconsecutive 16-bit signed integersInt16Array
bin.i32Arrayconsecutive 32-bit signed integersInt32Array
bin.f32Arrayconsecutive 32-bit floatsFloat32Array
bin.f64Arrayconsecutive 64-bit floatsFloat64Array