Bencode (pronounced B-Encode, not Ben Code) is a serialization encoding format used in torrent files for the BitTorrent protocol. It consists of a series of strings, integers, lists, and dictionaries; since lists and dictionaries contain multiple elements of the Bencode types, they can be nested hierarchically, encapsulating complex data structures.
The elements are:
- Strings: coded as a decimal number giving the length of the string, then a colon, then the string itself; e.g., 5:stuff
- Integers: coded as the letter "i", then the integer (as a series of decimal digits), then the letter "e"' e.g., i42e
- Lists: coded as the letter "l", then the list elements (each encoded as one of the Bencode types, which can include other lists), then the letter "e"; e.g., li42e5:stuffi666ee (which contains 42, 'stuff', and 666 as its elements)
- Dictionaries: pairs of keys and values, where the key is a string and the value can be any Bencode type; surrounded by an opening letter 'd' and closing letter 'e'; keys must appear in alphabetic order; e.g., d4:testi42e3:zzz4:junke is a dictionary where key 'test' has value 42 and key 'zzz' has value 'junk'.