Binary
MessagePack binary (bin) formats encode raw byte sequences without any UTF-8 requirement. bin 8 (0xc4), bin 16 (0xc5), and bin 32 (0xc6) cover 0-255, 0-65535, and 0-2^32-1 bytes respectively. Use bin for non-text data – cryptographic keys, images, file contents.
Description
The bin format was introduced in the MessagePack v2 spec to clearly separate binary data from text strings. Before v2, both were encoded as 'raw' with the same format bytes, requiring out-of-band knowledge to distinguish text from binary.
bin 8 (0xc4): 1 prefix byte + 1 byte length + data bin 16 (0xc5): 1 prefix byte + 2 byte length + data bin 32 (0xc6): 1 prefix byte + 4 byte length + data
Use cases: binary fields that would require base64 in JSON, cryptographic values (hashes, signatures, keys), image thumbnails embedded in messages, and protocol-specific byte sequences.
Format Variants
| Name | First Byte | Range | Description |
|---|---|---|---|
| bin 8 | 0xc4 | 0-255 bytes | 1-byte length follows, then data bytes. |
| bin 16 | 0xc5 | 0-65535 bytes | 2-byte big-endian length follows, then data. |
| bin 32 | 0xc6 | 0-2^32-1 bytes | 4-byte big-endian length follows, then data. |
Examples
| Label | Hex | Value |
|---|---|---|
| Empty | c4 00 | bin8, length=0 |
| 0xDEADBEEF | c4 04 de ad be ef | bin8, 4 bytes |
| SHA-256 | c4 20 [32 bytes] | bin8, 32-byte hash |