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 constructor | Encoded as | JavaScript value |
---|---|---|
bin.u8Array | consecutive 8-bit unsigned integers | Uint8Array |
bin.u8ClampedArray | consecutive 8-bit unsigned integers | Uint8ClampedArray |
bin.u16Array | consecutive 16-bit unsigned integers | Uint16Array |
bin.u32Array | consecutive 32-bit unsigned integers | Uint32Array |
bin.i8Array | consecutive 8-bit signed integers | Int8Array |
bin.i16Array | consecutive 16-bit signed integers | Int16Array |
bin.i32Array | consecutive 32-bit signed integers | Int32Array |
bin.f32Array | consecutive 32-bit floats | Float32Array |
bin.f64Array | consecutive 64-bit floats | Float64Array |