Skip to main content
2

Length-Delimited

tag = (field_number << 3) | 2. Followed by varint length, then that many bytes.

Wire type 2 (length-delimited) encodes a varint byte count followed by that many bytes. Used for string, bytes, embedded messages, and packed repeated fields. This is the most versatile wire type – it carries all variable-length data in Protobuf.

Used For

stringbytesembedded messagepacked repeated fields (int32, int64, float, double, etc.)

Encoding Details

Wire type 2 is the most important wire type in Protocol Buffers. After the field tag, a varint encodes the byte count of the payload, followed by exactly that many bytes.

Field types using wire type 2: string: UTF-8 encoded text bytes: raw byte sequence embedded messages: another Protobuf message serialized inline packed repeated: multiple values of a numeric type concatenated (no tag per element)

Embedded messages: when a field type is another message, the inner message is serialized and the bytes are placed in the wire type 2 payload. This is how Protobuf represents nested structures.

Packed repeated fields: instead of encoding each element with its own tag, packed repeated concatenates all values: the outer field tag appears once, followed by the total byte count, followed by all values concatenated. This is the default for proto3 numeric repeated fields.

Examples

LabelHexExplanation
"testing", field 212 07 74 65 73 74 69 6e 67Tag 0x12 = field 2, wire 2. Length 0x07 = 7. UTF-8 'testing'.
Embedded message1a 03 08 96 01Tag 0x1a = field 3, wire 2. Length 3. Inner message: field 1, varint 150.
Packed int32 [1,2,3]0a 03 01 02 03Tag 0x0a = field 1, wire 2. Length 3. Values 1, 2, 3 as varints.

See Also