Length-Delimited
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
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
| Label | Hex | Explanation |
|---|---|---|
| "testing", field 2 | 12 07 74 65 73 74 69 6e 67 | Tag 0x12 = field 2, wire 2. Length 0x07 = 7. UTF-8 'testing'. |
| Embedded message | 1a 03 08 96 01 | Tag 0x1a = field 3, wire 2. Length 3. Inner message: field 1, varint 150. |
| Packed int32 [1,2,3] | 0a 03 01 02 03 | Tag 0x0a = field 1, wire 2. Length 3. Values 1, 2, 3 as varints. |