Kit
Functions

getI64Codec

getI64Codec(config): FixedSizeCodec<number | bigint, bigint, 8>

Returns a codec for encoding and decoding 64-bit signed integers (i64).

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

Parameters

ParameterTypeDescription
configNumberCodecConfigOptional configuration to specify endianness (little by default).

Returns

FixedSizeCodec<number | bigint, bigint, 8>

A FixedSizeCodec<number | bigint, bigint, 8> for encoding and decoding i64 values.

Examples

Encoding and decoding an i64 value.

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

Using big-endian encoding.

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

Remarks

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

Separate getI64Encoder and getI64Decoder functions are available.

const bytes = getI64Encoder().encode(-42);
const value = getI64Decoder().decode(bytes);

See

On this page