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
| Name | First Byte | Range | Description |
|---|---|---|---|
| fixstr | 0xa0-0xbf | 0-31 bytes | Length in lower 5 bits. No separate length byte. |
| str 8 | 0xd9 | 0-255 bytes | 1-byte length follows. |
| str 16 | 0xda | 0-65535 bytes | 2-byte big-endian length follows. |
| str 32 | 0xdb | 0-2^32-1 bytes | 4-byte big-endian length follows. |
Examples
| Label | Hex | Value |
|---|---|---|
| "" | a0 | empty string (fixstr, len=0) |
| "a" | a1 61 | fixstr len=1, 0x61='a' |
| "hello" | a5 68 65 6c 6c 6f | fixstr len=5 |
| 32-byte str | d9 20 [32 bytes] | str8, length=32 |