Skip to main content
3

Text String

0x60 – 0x7b (0x7f indefinite)RFC 8949

CBOR major type 3 encodes UTF-8 text strings. The argument gives the byte count of the UTF-8 encoding, followed by that many bytes. 0x65 0x68 0x65 0x6c 0x6c 0x6f = 5-byte string 'hello'. CBOR text strings MUST be valid UTF-8.

Encoding

Initial byte: 0b011_XXXXX. Followed by UTF-8 bytes of the string.

Major type 3 is the text string type, equivalent to JSON strings. Unlike JSON which uses backslash escaping, CBOR text strings are raw UTF-8 bytes with a byte length prefix. No escaping is needed – any valid UTF-8 sequence is valid.

The RFC 8949 well-formedness rule requires text strings to be valid UTF-8. Implementations SHOULD validate. Byte strings (major type 2) exist for data that is not valid UTF-8 or should not be interpreted as text.

Key difference from JSON: CBOR text strings are length-prefixed, so parsers do not need to scan for a closing delimiter. This makes CBOR parsing significantly faster than JSON for large strings.

Examples

LabelHexValue
Empty60""
"a"61 61"a"
"hello"65 68 65 6c 6c 6f"hello"
"IETF"64 49 45 54 46"IETF" (4 bytes)

See Also