Byte-slice coder for binary structures.
Encodes/decodes raw bytes either as a fixed-length slice or as a variable-length view that consumes all available bytes.
It's the user's responsibility to provide a buffer big enough to fit the whole data.
Fixed and variable length
Fixed and variable length
import { assertEquals } from "@std/assert"; import { bytes } from "@hertzg/binstruct/bytes"; const fixed = bytes(4); const variable = bytes(); const input = new Uint8Array([1, 2, 3, 4, 5]); const buf = new Uint8Array(32); const w1 = fixed.encode(input, buf); const [d1] = fixed.decode(buf); assertEquals(Array.from(d1), [1, 2, 3, 4]); const w2 = variable.encode(input, buf); const [d2] = variable.decode(buf); assertEquals(Array.from(d2.slice(0, input.length)), Array.from(input)); assertEquals(typeof w1, "number"); assertEquals(typeof w2, "number");
f
bytes(lengthOrRef?: LengthOrRef | null): Coder<Uint8Array>
Creates a Coder for byte slices.
Usage
import * as mod from "bytes/bytes.ts";