getI256Codec

function getI256Codec(
    config?,
): FixedSizeCodec<number | bigint, bigint, 32>;

Returns a codec for encoding and decoding 256-bit signed integers (i256).

This codec serializes i256 values using 32 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, 32>

A FixedSizeCodec<number | bigint, bigint, 32> for encoding and decoding i256 values.

Examples

Encoding and decoding an i256 value.

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

Using big-endian encoding.

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

Remarks

This codec supports values between -2^255 and 2^255 - 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 getI128Codec or getI64Codec.
  • If you need a larger signed integer, consider using a custom codec.
  • If you need unsigned integers, consider using getU256Codec.

Separate getI256Encoder and getI256Decoder functions are available.

const bytes = getI256Encoder().encode(-42);
const value = getI256Decoder().decode(bytes);

See

On this page