= Length-prefixed encoding
A \b[length-prefixed encoding] represents a variable-size field by writing a length before its payload. The length's units, width, <endianness>, permitted range and inclusion or exclusion of the prefix itself are part of the <serialization> format. For example, the <Transport Layer Security> presentation language counts vector lengths in bytes and encodes variable-length vectors with a length field whose width follows the specified upper bound.<The Transport Layer Security Protocol Version 1.3 (RFC 8446)>[§§3.3–3.4, numbers and vectors]{ref}
An original two-field format uses one unsigned byte for each payload length:
$$
E(x,y)=\operatorname{len}_8(x)\Vert x\Vert\operatorname{len}_8(y)\Vert y,
\qquad 0\le |x|,|y|\le255.
$$
Here $|x|$ counts payload bytes and $\Vert$ is <concatenation>. The ASCII fields `ab` and `c` give `02 61 62 01 63`, while `a` and `bc` give `01 61 02 62 63`. Bare concatenation would give `61 62 63` in both cases. This distinction is useful when a <cryptographic hash function> or <message authentication code> must commit to two separate fields.
The encoding is an <injective function> on its stated input set. Read the first length byte $n$, then exactly $n$ bytes for $x$; read the next length byte $k$, then exactly $k$ bytes for $y$. This reconstructs both fields uniquely. For a complete two-field record, require that no bytes remain. Missing bytes, absent length fields or forbidden lengths are rejected rather than silently reinterpreted. The proof depends on these parsing rules, not merely on putting some number before the data.
A <fixed-width length field> also imposes a representable limit: 256 payload bytes cannot be encoded by the one-byte scheme above. Truncating that length modulo 256 would make the parser see zero and destroy the intended format. A larger format must specify a larger prefix or a different encoding.
Finally, byte count and text-character count can differ. The text `é` has two bytes `c3 a9` in <UTF-8>, so its one-byte length prefix here is `02`. A length prefix provides boundaries; <cryptographic domain separation> may additionally require a field type, role label or protocol version to distinguish records with different meanings.
Back to article page