Bencode (pronounced likeBee-encode) is the encoding used by thepeer-to-peer file sharing systemBitTorrent for storing and transmitting loosely structured data.[1]
It supports four different types of values:
Bencoding is most commonly used intorrent files, and as such is part of the BitTorrent specification. Thesemetadata files are simply bencoded dictionaries.
Bencoding is simple and (because numbers are encoded as text in decimal notation) is unaffected byendianness, which is important for across-platform application like BitTorrent. It is also fairly flexible, as long as applications ignore unexpected dictionary keys, so that new ones can be added without creating incompatibilities.
Bencode uses ASCII characters as delimiters and digits to encode data structures in a simple and compact format.
i<base10 integer>e.i0e.i42e.i-42e.<length>:<contents>.:) separates the length and the contents.0:.7:bencode.l<elements>e.l and ends withe.le.l7:bencodei-20ee.d<pairs>e.d and ends withe.de.d7:meaningi42e4:wiki7:bencodee.There are no restrictions on the types of values stored within lists and dictionaries; they may contain other lists and dictionaries, allowing for arbitrarily complex data structures.
Bencode defines only byte string types, rather than any particularcharacter encoding for storing text. Downstream applications and data format specifications that use bencode are free to specify whichever encoding they prefer for encoding text into bencoded byte strings.
Here is the list of the possible errors that a ill-formatted bencode may have:
Bencode is a very specialized kind of binary coding with some unique properties:
Bencode is not considered ahuman-readable encoding format. While the BE codegroups can be decoded manually, the bencoded values often containbinary data, so decoding by hand may be error prone. It is not safe to edit bencode files in text editors because bencoded files contain binary data, so ahex editor or specialised bencode editor tool must be used.
Bencode does not store any metadata about the size of list or dictionary structures, requiring all preceding elements to be read sequentially in order to reach a particular field. As such, bencode may not be suitable for large data structures whererandom access to fields is required.