Kit
Functions

getBytesCodec

getBytesCodec(): VariableSizeCodec<Uint8Array<ArrayBufferLike> | ReadonlyUint8Array, ReadonlyUint8Array>

Returns a codec for encoding and decoding raw byte arrays.

This codec serializes and deserializes byte arrays without modification.

The size of the encoded and decoded byte array is determined dynamically. This means, when reading, the codec will consume all remaining bytes in the input.

Returns

VariableSizeCodec<Uint8Array<ArrayBufferLike> | ReadonlyUint8Array, ReadonlyUint8Array>

A VariableSizeCodec<ReadonlyUint8Array | Uint8Array, ReadonlyUint8Array>.

Example

Encoding and decoding a byte array.

const codec = getBytesCodec();
 
codec.encode(new Uint8Array([1, 2, 3])); // 0x010203
codec.decode(new Uint8Array([255, 0, 127])); // Uint8Array([255, 0, 127])

Remarks

Separate getBytesEncoder and getBytesDecoder functions are available.

const bytes = getBytesEncoder().encode(new Uint8Array([1, 2, 3]));
const value = getBytesDecoder().decode(bytes);

See

On this page