Kit
Variables

getU16Codec

const getU16Codec: (config?) => FixedSizeCodec<bigint | number, number, 2>

Returns a codec for encoding and decoding 16-bit unsigned integers (u16).

This codec serializes u16 values using two bytes in little-endian format by default. You may specify big-endian storage using the endian option.

Parameters

ParameterTypeDescription
config?NumberCodecConfigOptional settings for endianness.

Returns

FixedSizeCodec<bigint | number, number, 2>

A FixedSizeCodec<number | bigint, number, 2> for encoding and decoding u16 values.

Examples

Encoding and decoding a u16 value.

const codec = getU16Codec();
const bytes = codec.encode(42); // 0x2a00 (little-endian)
const value = codec.decode(bytes); // 42

Storing values in big-endian format.

const codec = getU16Codec({ endian: Endian.Big });
const bytes = codec.encode(42); // 0x002a

Remarks

This codec supports values between 0 and 2^16 - 1. If you need a larger range, consider using getU32Codec or getU64Codec. For signed integers, use getI16Codec.

Separate getU16Encoder and getU16Decoder functions are available.

const bytes = getU16Encoder().encode(42);
const value = getU16Decoder().decode(bytes);

See

On this page