Skip to content
Message Encoding
step 1/5

Reading — step 1 of 5

Read

~1 min readWire Format

Message Encoding

A message is just a sequence of field encodings.

message Person {
    string name = 1;
    int32 age = 2;
    bool active = 3;
}

For Person(name="Alice", age=30, active=true):

field 1 (string "Alice"):
  tag: (1<<3)|2 = 0x0A
  length: 5
  value: "Alice"

field 2 (int32 30):
  tag: (2<<3)|0 = 0x10
  value: 30 = 0x1E

field 3 (bool true):
  tag: (3<<3)|0 = 0x18
  value: 1 = 0x01

Total bytes: 0a 05 41 6c 69 63 65 10 1e 18 01

10 bytes for a message that JSON would be ~50 bytes.

Decoding:

  1. Read tag (varint).
  2. Field number = tag >> 3; wire_type = tag & 7.
  3. Based on wire type:
    • 0: read varint as value.
    • 1: read 8 bytes as fixed64.
    • 2: read varint length, then that many bytes.
    • 5: read 4 bytes as fixed32.
  4. Look up field in schema, set message field.
  5. Repeat until end of buffer.
python

Repeated:

  • Multiple occurrences of same tag → append to list.
  • Packed (proto3 default): one tag, length-delimited concatenated values.

Maps:

  • Encoded as repeated message with key + value fields.
  • Schema sugar.

Discussion

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

Sign in to post a comment or reply.

Loading…