Reading — step 1 of 5
Read
~1 min readFormat Basics
Primitive Types
Nil: 0xc0 (one byte).
Boolean: 0xc2 (false), 0xc3 (true).
Integers:
- positive fixint (
0xxxxxxx): 0..127 inline (1 byte total). - negative fixint (
111xxxxx): -32..-1 inline (1 byte). - uint 8:
0xcc, value byte. - uint 16:
0xcd, 2 BE bytes. - uint 32:
0xce, 4 BE bytes. - uint 64:
0xcf, 8 BE bytes. - int 8:
0xd0, value byte. - int 16:
0xd1, 2 BE bytes. - int 32:
0xd2, 4 BE bytes. - int 64:
0xd3, 8 BE bytes.
Encoder picks smallest representation:
python
Float 32: 0xca, 4 BE bytes (IEEE 754).
Float 64: 0xcb, 8 BE bytes (IEEE 754).
Default: encoders use float64 for Python floats (preserve precision).
Strings:
- fixstr (
101xxxxx): 0-31 bytes, length in low 5 bits. - str 8:
0xd9, 1-byte length (up to 255). - str 16:
0xda, 2-byte length. - str 32:
0xdb, 4-byte length. - Then UTF-8 bytes.
Binary (raw bytes, distinct from string):
- bin 8:
0xc4, 1-byte length. - bin 16:
0xc5, 2-byte length. - bin 32:
0xc6, 4-byte length.
Pre-2013 spec didn't have separate bin; everything was raw. Modern spec separates.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…