Skip to content
String Encoding
step 1/5

Reading — step 1 of 5

Read

~1 min readFormat Basics

String Encoding

Strings in MessagePack are UTF-8 bytes prefixed by length.

  • fixstr: 0xa0..0xbf — top 3 bits 101, low 5 bits = length (0-31). Then bytes.
  • str 8: 0xd9 + 1 byte length (0-255). Then bytes.
  • str 16: 0xda + 2 byte length BE. Then bytes.
  • str 32: 0xdb + 4 byte length BE. Then bytes.

Encoding "hello" (5 bytes):

  • fixstr: 0xa5 (length 5 in low bits) + 68 65 6c 6c 6f = 6 bytes total.

Encoding 50-byte string:

  • str 8: 0xd9 + 0x32 (50) + 50 bytes = 52 bytes.
python

Length is in BYTES (not characters): UTF-8 multi-byte chars affect length.

UTF-8 encoding:

  • ASCII (≤ 127): 1 byte.
  • Latin / extended: 2 bytes.
  • BMP: 3 bytes.
  • Supplementary: 4 bytes.

"héllo" (é = 2 bytes UTF-8) = 6 bytes.

Strings vs binary:

  • Strings: UTF-8 text. Receivers can decode as text.
  • Binary: arbitrary bytes. Don't try to decode.

Examples:

  • Encoding image bytes → use bin format.
  • Encoding JSON-style text → use str.

Some old MessagePack libs used "raw" type for both strings and binary. Modern spec separates with bin types.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…