getU256Codec

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

Returns a codec for encoding and decoding 256-bit unsigned integers (u256).

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

Examples

Encoding and decoding a u256 value.

const codec = getU256Codec();
const bytes = codec.encode(42); // 0x2a00000000000000000000000000000000000000000000000000000000000000
const value = codec.decode(bytes); // 42n

Using big-endian encoding.

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

Remarks

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

Separate getU256Encoder and getU256Decoder functions are available.

const bytes = getU256Encoder().encode(42);
const value = getU256Decoder().decode(bytes);

See

On this page