Kit
Variables

getU128Codec

const getU128Codec: (config?) => FixedSizeCodec<bigint | number, bigint, 16>

Returns a codec for encoding and decoding 128-bit unsigned integers (u128).

This codec serializes u128 values using 16 bytes. Values can be provided as either number or bigint, but the decoded value is always a bigint.

Parameters

ParameterTypeDescription
config?NumberCodecConfigOptional configuration to specify endianness (little by default).

Returns

FixedSizeCodec<bigint | number, bigint, 16>

A FixedSizeCodec<number | bigint, bigint, 16> for encoding and decoding u128 values.

Examples

Encoding and decoding a u128 value.

const codec = getU128Codec();
const bytes = codec.encode(42); // 0x2a000000000000000000000000000000
const value = codec.decode(bytes); // 42n

Using big-endian encoding.

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

Remarks

This codec supports values between 0 and 2^128 - 1. Since JavaScript number cannot safely represent values beyond 2^53 - 1, the decoded value is always a bigint.

Separate getU128Encoder and getU128Decoder functions are available.

const bytes = getU128Encoder().encode(42);
const value = getU128Decoder().decode(bytes);

See

On this page