Unsigned Integer
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
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
| Label | Hex | Value |
|---|---|---|
| 0 | 00 | 0 |
| 1 | 01 | 1 |
| 23 | 17 | 23 (max direct-encoded) |
| 24 | 18 18 | 24 (one extra byte) |
| 255 | 18 ff | 255 |
| 256 | 19 01 00 | 256 (two extra bytes) |
| 65536 | 1a 00 01 00 00 | 65536 |