Kit
Variables

getI128Codec

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

Returns a codec for encoding and decoding 128-bit signed integers (i128).

This codec serializes i128 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 i128 values.

Examples

Encoding and decoding an i128 value.

const codec = getI128Codec();
const bytes = codec.encode(-42n); // 0xd6ffffffffffffffffffffffffffffff
const value = codec.decode(bytes); // -42n

Using big-endian encoding.

const codec = getI128Codec({ endian: Endian.Big });
const bytes = codec.encode(-42n); // 0xffffffffffffffffffffffffffffd6

Remarks

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

  • If you need a smaller signed integer, consider using getI64Codec or getI32Codec.
  • If you need a larger signed integer, consider using a custom codec.
  • If you need unsigned integers, consider using getU128Codec.

Separate getI128Encoder and getI128Decoder functions are available.

const bytes = getI128Encoder().encode(-42);
const value = getI128Decoder().decode(bytes);

See

On this page