What is Bencode?
Bencode is a compact serialization format comparable to JSON. It is used heavily in the BitTorrent ecosystem. For most use cases, it will result in a smaller encoding than JSON.
Bencode supports a few common data types; integer, byte array / string, list and dictionary.
Integers are encoded using the
i prefix followed by the decimal representation of the number and the
i42e is the Bencode representation of the number
Byte array / string
Byte arrays and strings are encoded using the length of the array/string followed by a colon
: and then the raw data.
4:spam is the Bencode representation of the string
Lists are encoded using the
l prefix followed by the Bencode representations of the items in the list and the
li42ee is the Bencode representation of the list
Dictionaries are encoded using the
d prefix followed by the Bencode representations of the keys and values in the dictionary and the
The keys must be byte arrays or strings and must be sorted in lexicographical order.
d3:fooi42ee is the Bencode representation of the dictionary