Kit
Functions

reverseCodec

reverseCodec<TFrom, TTo, TSize>(codec): FixedSizeCodec<TFrom, TTo, TSize>

Reverses the bytes of a fixed-size codec.

Given a FixedSizeCodec, this function returns a new FixedSizeCodec that reverses the bytes within the fixed-size byte array during encoding and decoding.

This can be useful to modify endianness or for other byte-order transformations.

Type Parameters

Type ParameterDescription
TFromThe type of the value to encode.
TToThe type of the decoded value.
TSize extends numberThe fixed size of the encoded/decoded value in bytes.

Parameters

ParameterTypeDescription
codecFixedSizeCodec<TFrom, TTo, TSize>The fixed-size codec to reverse.

Returns

FixedSizeCodec<TFrom, TTo, TSize>

A new codec that encodes and decodes bytes in reverse order.

Example

Reversing a u16 codec.

const codec = reverseCodec(getU16Codec({ endian: Endian.Big }));
const bytes = codec.encode(0x1234); // 0x3412 (bytes are flipped)
const value = codec.decode(bytes);  // 0x1234 (bytes are flipped back)

Remarks

If you only need to reverse an encoder, use reverseEncoder. If you only need to reverse a decoder, use reverseDecoder.

const bytes = reverseEncoder(getU16Encoder()).encode(0x1234);
const value = reverseDecoder(getU16Decoder()).decode(bytes);

See

On this page