Protobuf field guide

Read the Protobuf wire format.

A Protobuf message is a sequence of numbered fields. Walk through a small message byte by byte, then use that structure to inspect your own payload.

Start with one integer.

This proto3 definition has a single uint32 field numbered 1. Encoding the JSON object {"id":150} produces the three bytes 08 96 01. You can paste the definition into the schema workspace and reproduce the result.

Protocol definition & example
syntax = "proto3";

message Example {
  uint32 id = 1;
}

// JSON: {"id": 150}
// Hex:  08 96 01

The first varint identifies the field.

A tag combines the field number and wire type: tag = (field_number << 3) | wire_type. For 08, the numeric tag is 8. Shifting right by three gives field 1; the low three bits give wire type 0, a varint.

Tags are themselves varints, so a tag is not always one byte long. Field names such as id are not present in the payload.

Wire typeEncodingCommon uses
0Varintint32, int64, uint32, uint64, sint32, sint64, bool, enum
164-bitfixed64, sfixed64, double
2Length-delimitedstring, bytes, messages, packed repeated values
532-bitfixed32, sfixed32, float

Read 96 01 as the integer 150.

A varint uses seven data bits per byte. The high bit says whether another byte follows. In 96, the high bit is set and the lower seven bits are 0x16, or 22. The next byte is 01, so the result is 22 + (1 << 7) = 150.

This interpretation is an unsigned integer. A schema is still needed to distinguish ordinary integers, bools, enums and ZigZag-encoded sint values. For example, a raw varint value of 1 represents -1 when its declared type is sint32.

Byte walkthrough
96          01
10010110    00000001
 0010110     0000001

22 + (1 * 128) = 150

A length tells you where the next field begins.

For field 2 with wire type 2, the tag is 12 in Hex. The next varint gives the byte length. The bytes 12 02 48 69 therefore contain field 2 with a two-byte value, which can be interpreted as the UTF-8 text Hi.

A length counts bytes, not visible characters. UTF-8 characters may take multiple bytes. The same outer encoding is also used for embedded messages, raw bytes and packed scalar arrays.

Protocol definition & example
syntax = "proto3";
message Greeting {
  uint32 id = 1;
  string text = 2;
}

// JSON: {"id":150,"text":"Hi"}
// Hex:  08 96 01 12 02 48 69

Structure is observable. Meaning needs a schema.

Without a .proto definition, you can identify field numbers, wire types and encoded lengths. You cannot reliably recover names, choose among scalar types that share a wire type, or determine whether an arbitrary length-delimited value was meant to be a nested message.

Field order is not a reliable definition of meaning, and byte-for-byte equality is not a universal test of message equality. Different valid serializations can represent the same logical data. Use a schema-aware interpretation when correctness matters.

Reference material

Use the specifications for details beyond these examples.