Bencode Format: Torrent Data Structures Explained

Bencoding is the serialization format used by the BitTorrent protocol to structure metadata in .torrent files and manage tracker communications. Designed to be both human-readable and computationally lightweight, Bencoding supports exactly four fundamental data types: byte strings, integers, lists, and dictionaries. This guide outlines the exact specification, syntax rules, and encoding examples for each of these four core data structures.

1. Integers

Integers are encoded using an ASCII format delimited by a leading i and a trailing e.

2. Byte Strings

Byte strings (often referred to simply as strings) do not use a starting delimiter. Instead, they are prefixed with their byte length followed by a colon and the raw byte data.

3. Lists

Lists are ordered collections of values. A list begins with the character l (lowercase L) and terminates with the character e.

4. Dictionaries

Dictionaries represent associative arrays (key-value maps). A dictionary begins with the character d and terminates with the character e.