Skip to main content
0

Unsigned Integer

0x00 – 0x1bRFC 8949

CBOR major type 0 encodes unsigned integers. The lower 5 bits (additional info) encode the value directly if 0-23, or indicate 1/2/4/8 following bytes for larger values. 0x00 = 0, 0x17 = 23, 0x18 0x18 = 24, 0x1a 0x00 0x01 0x00 0x00 = 65536.

Encoding

Initial byte: 0b000_XXXXX where XXXXX = additional info

Major type 0 uses the most compact possible encoding. For values 0-23, the entire integer fits in a single byte (upper 3 bits = 000, lower 5 bits = value). For 24-255, one additional byte follows (additional info = 24, 0x18). For 256-65535, two bytes follow. For 65536-4294967295, four bytes follow. For larger values, eight bytes follow.

The additional info field (lower 5 bits) acts as follows: 0-23: value is directly in additional info 24 (0x18): 1-byte uint follows 25 (0x19): 2-byte uint follows (big-endian) 26 (0x1a): 4-byte uint follows (big-endian) 27 (0x1b): 8-byte uint follows (big-endian) 28-30: reserved 31 (0x1f): break code (for indefinite-length structures, not used for integers)

Examples

LabelHexValue
0000
1011
231723 (max direct-encoded)
2418 1824 (one extra byte)
25518 ff255
25619 01 00256 (two extra bytes)
655361a 00 01 00 0065536

See Also