Variables
getShortU16Codec
const
getShortU16Codec: () =>VariableSizeCodec
<bigint
|number
,number
>
Returns a codec for encoding and decoding shortU16
values.
It serializes unsigned integers using 1 to 3 bytes based on the encoded value. The larger the value, the more bytes it uses.
- If the value is
<= 0x7f
(127), it is stored in a single byte and the first bit is set to0
to indicate the end of the value. - Otherwise, the first bit is set to
1
to indicate that the value continues in the next byte, which follows the same pattern. - This process repeats until the value is fully encoded in up to 3 bytes. The third and last byte, if needed, uses all 8 bits to store the remaining value.
In other words, the encoding scheme follows this structure:
Returns
VariableSizeCodec
<bigint
| number
, number
>
A VariableSizeCodec<number | bigint, number>
for encoding and decoding shortU16
values.
Example
Encoding and decoding shortU16
values.
Remarks
This codec efficiently stores small numbers, making it useful for transactions and compact representations.
If you need a fixed-size u16
codec, consider using getU16Codec.
Separate getShortU16Encoder and getShortU16Decoder functions are available.