The shape of CBOR bytes
CBOR carries a set of values like JSON, but every value states its own kind and length in binary. The first byte tells you what kind of value starts and how long it is, so a reader can walk the whole item without a schema.
First byte = major type (top 3 bits) + additional information (low 5 bits)
0x82 has 100 (4) in the top three bits and 00010 (2) in the low five: major type 4, an array of two items. 0x64 0x61 0x62 0x63 0x64 is major type 3 with length 4 and the body "abcd".
| Major type | Holds | What the length means |
|---|---|---|
| 0 | unsigned integer | the argument is the value (0-23) or 1, 2, 4 or 8 following bytes |
| 1 | negative integer | value = -1 - n |
| 2 | byte string | number of body bytes |
| 3 | text string | number of UTF-8 body bytes |
| 4 | array | number of items |
| 5 | map | number of key-value pairs |
| 6 | tag | the tag number, then one value |
| 7 | simple value or float | false, true, null, undefined, or Float16/32/64 |
Additional information 24-31
A value of 23 or less sits in the low five bits. 24 reads one more byte, 25 two, 26 four and 27 eight. 28 to 30 are reserved, and 31 marks an indefinite-length item that carries no length.
Definite and indefinite
Arrays, maps and strings come either with a length up front (definite) or starting with 0x9f and ending with 0xff (indefinite). Indefinite form suits streaming, but because the length is not known in advance deterministic encoding never uses it.
Integers and floats
- An unsigned 64-bit integer runs from 0 to 18,446,744,073,709,551,615. This tool keeps values above 2^53 as strings so no precision is lost.
- Negative numbers are written as -1 - n: -1 is
0x20, -500 is0x3901f3. - Floats come in three widths: Float16 (
0xf9), Float32 (0xfa) and Float64 (0xfb). 1.5 is0xf9 0x3e 0x00in Float16.
Tags add meaning
A tag does not change how the inner value is read; it adds meaning. Tags 0 and 1 are dates and times, 2 and 3 are big integers, 18 is COSE_Sign1, and 24 says the inner byte string is itself a CBOR data item. This tool shows the name of the well-known tags and the interpreted value (date, bignum) beside them.
CBOR separates UTF-8 text from bytes. When a text string is not valid UTF-8 this tool reports an error and shows the remaining bytes with replacement characters.
Questions
Why use CBOR at all?
It is smaller than JSON, states its own length without a schema, and carries byte strings, large integers and tags directly. Security standards such as COSE and CWT rely on those properties.
Why does major type 7 hold so much?
It carries the four simple values (false, true, null, undefined), reserved simple values, and floats in three widths. The width comes from additional information 25, 26 or 27.
Why separate text from bytes?
Text can be validated as UTF-8 while bytes hold arbitrary binary data. Blending them would leave the receiver nothing to validate.
The basic-map preset is a 109-byte example holding integers, text, a Float16, an array, a nested map and a byte string at once.
Written against RFC 8949 (CBOR), RFC 9052 (COSE), RFC 8392 (CWT) and the IANA COSE algorithm registry. Every byte string and key here is synthetic.