Skip to main content

Integer

MessagePack integer formats cover the full signed/unsigned 64-bit integer range. Small positive integers (0-127) encode in a single byte (positive fixint). Small negative integers (-32 to -1) also encode in a single byte (negative fixint). Larger values use uint8/uint16/uint32/uint64 or int8/int16/int32/int64 formats.

Description

MessagePack's integer encoding is its most compact feature. The positive fixint format stores integers 0-127 in a single byte with no prefix – the byte value IS the integer. This means the most common small positive integers cost only 1 byte total.

The negative fixint format uses bytes 0xe0-0xff (the high 3 bits are 111) to encode integers -32 to -1 in a single byte.

For larger values, prefix bytes indicate the type: 0xcc = uint8 (1-byte unsigned follows) 0xcd = uint16 (2-byte big-endian unsigned) 0xce = uint32 (4-byte big-endian unsigned) 0xcf = uint64 (8-byte big-endian unsigned) 0xd0 = int8 (1-byte signed two's complement) 0xd1 = int16 (2-byte big-endian signed) 0xd2 = int32 (4-byte big-endian signed) 0xd3 = int64 (8-byte big-endian signed)

Format Variants

NameFirst ByteRangeDescription
positive fixint0x00-0x7f0 to 127Value is the byte itself. No prefix. Single byte.
negative fixint0xe0-0xff-32 to -1High 3 bits = 111. Value = byte - 256.
uint 80xcc0 to 2551 prefix byte + 1 data byte.
uint 160xcd0 to 655351 prefix byte + 2 data bytes (big-endian).
uint 320xce0 to 2^32-11 prefix byte + 4 data bytes (big-endian).
uint 640xcf0 to 2^64-11 prefix byte + 8 data bytes (big-endian).
int 80xd0-128 to 1271 prefix byte + 1 signed byte.
int 160xd1-32768 to 327671 prefix byte + 2 signed bytes (big-endian).
int 320xd2-2^31 to 2^31-11 prefix byte + 4 signed bytes (big-endian).
int 640xd3-2^63 to 2^63-11 prefix byte + 8 signed bytes (big-endian).

Examples

LabelHexValue
000positive fixint 0
1277fpositive fixint 127 (max direct)
128cc 80uint8 128
-1ffnegative fixint -1
-32e0negative fixint -32 (min direct)
-33d0 dfint8 -33
256cd 01 00uint16 256

See Also