Skip to main content

String

MessagePack string formats encode UTF-8 text. fixstr (0xa0-0xbf) encodes strings 0-31 bytes in a single prefix byte containing the length. str 8, str 16, and str 32 support longer strings with 1, 2, or 4 bytes of length prefix respectively. Strings must be valid UTF-8.

Description

MessagePack has four string formats differentiated by maximum length: fixstr (0xa0-0xbf): 0-31 bytes. Length in lower 5 bits of prefix byte. Most strings in practice. str 8 (0xd9): 0-255 bytes. 1-byte length prefix. str 16 (0xda): 0-65535 bytes. 2-byte length prefix. str 32 (0xdb): 0-2^32-1 bytes. 4-byte length prefix.

The length prefix counts bytes, not Unicode characters. A 3-byte emoji = length 3 in the prefix.

Historical note: the original MessagePack spec had a 'raw' type combining strings and binary. The current spec separates str (for text) and bin (for binary) to avoid the encoding ambiguity.

Format Variants

NameFirst ByteRangeDescription
fixstr0xa0-0xbf0-31 bytesLength in lower 5 bits. No separate length byte.
str 80xd90-255 bytes1-byte length follows.
str 160xda0-65535 bytes2-byte big-endian length follows.
str 320xdb0-2^32-1 bytes4-byte big-endian length follows.

Examples

LabelHexValue
""a0empty string (fixstr, len=0)
"a"a1 61fixstr len=1, 0x61='a'
"hello"a5 68 65 6c 6c 6ffixstr len=5
32-byte strd9 20 [32 bytes]str8, length=32

See Also