<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.sarg.dev/index.php?action=history&amp;feed=atom&amp;title=Bencode</id>
	<title>Bencode - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.sarg.dev/index.php?action=history&amp;feed=atom&amp;title=Bencode"/>
	<link rel="alternate" type="text/html" href="https://wiki.sarg.dev/index.php?title=Bencode&amp;action=history"/>
	<updated>2026-10-11T14:05:13Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.44.2</generator>
	<entry>
		<id>https://wiki.sarg.dev/index.php?title=Bencode&amp;diff=657676&amp;oldid=prev</id>
		<title>imported&gt;Thüringer: /* top */ missing space</title>
		<link rel="alternate" type="text/html" href="https://wiki.sarg.dev/index.php?title=Bencode&amp;diff=657676&amp;oldid=prev"/>
		<updated>2025-09-17T07:36:13Z</updated>

		<summary type="html">&lt;p&gt;&lt;span class=&quot;autocomment&quot;&gt;top: &lt;/span&gt; missing space&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{short description|Data serialization format}}&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Bencode&amp;#039;&amp;#039;&amp;#039; (pronounced like &amp;#039;&amp;#039;Bee-encode&amp;#039;&amp;#039;) is the encoding used by the [[peer-to-peer]] file sharing system [[BitTorrent (protocol)|BitTorrent]] for storing and transmitting loosely structured data.&amp;lt;ref&amp;gt;[https://www.bittorrent.org/beps/bep_0003.html The BitTorrent Protocol Specification] {{Webarchive|url=https://web.archive.org/web/20190726102940/http://www.bittorrent.org/beps/bep_0003.html |date=2019-07-26 }}. BitTorrent.org. Retrieved 8 October 2018.&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It supports four different types of values:&lt;br /&gt;
* [[string (computer science)|byte strings]],&lt;br /&gt;
* [[integer]]s,&lt;br /&gt;
* [[List (computing)|list]]s (arrays), and&lt;br /&gt;
* [[associative array|dictionaries (associative arrays)]].&lt;br /&gt;
&lt;br /&gt;
Bencoding is most commonly used in [[torrent file]]s, and as such is part of the BitTorrent specification. These [[metadata]] files are simply bencoded dictionaries.&lt;br /&gt;
&lt;br /&gt;
Bencoding is simple and (because numbers are encoded as text in decimal notation) is unaffected by [[endianness]], which is important for a [[cross-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.&lt;br /&gt;
&lt;br /&gt;
== Encoding Algorithm ==&lt;br /&gt;
&lt;br /&gt;
Bencode uses ASCII characters as delimiters and digits to encode data structures in a simple and compact format.&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Integers&amp;#039;&amp;#039;&amp;#039; are encoded as &amp;lt;code&amp;gt;i&amp;lt;base10 integer&amp;gt;e&amp;lt;/code&amp;gt;.&lt;br /&gt;
** The integer is encoded in base 10 and may be negative (indicated by a leading hyphen-minus).&lt;br /&gt;
** Leading zeros are not allowed unless the integer is zero.&lt;br /&gt;
** Examples:&lt;br /&gt;
*** Zero is encoded as &amp;lt;code&amp;gt;i0e&amp;lt;/code&amp;gt;.&lt;br /&gt;
*** The number 42 is encoded as &amp;lt;code&amp;gt;i42e&amp;lt;/code&amp;gt;.&lt;br /&gt;
*** Negative forty-two is encoded as &amp;lt;code&amp;gt;i-42e&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Byte Strings&amp;#039;&amp;#039;&amp;#039; are encoded as &amp;lt;code&amp;gt;&amp;lt;length&amp;gt;:&amp;lt;contents&amp;gt;&amp;lt;/code&amp;gt;.&lt;br /&gt;
** The length is the number of bytes in the string, encoded in base 10.&lt;br /&gt;
** A colon (&amp;lt;code&amp;gt;:&amp;lt;/code&amp;gt;) separates the length and the contents.&lt;br /&gt;
** The contents are the exact number of bytes specified by the length.&lt;br /&gt;
** The contents are a sequence of bytes (not a textual string)&lt;br /&gt;
** Examples:&lt;br /&gt;
*** An empty string is encoded as &amp;lt;code&amp;gt;0:&amp;lt;/code&amp;gt;.&lt;br /&gt;
*** The string &amp;quot;bencode&amp;quot; is encoded as &amp;lt;code&amp;gt;7:bencode&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Lists&amp;#039;&amp;#039;&amp;#039; are encoded as &amp;lt;code&amp;gt;l&amp;lt;elements&amp;gt;e&amp;lt;/code&amp;gt;.&lt;br /&gt;
** Begins with &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt; and ends with &amp;lt;code&amp;gt;e&amp;lt;/code&amp;gt;.&lt;br /&gt;
** Elements are bencoded values concatenated without delimiters.&lt;br /&gt;
** Examples:&lt;br /&gt;
*** An empty list is encoded as &amp;lt;code&amp;gt;le&amp;lt;/code&amp;gt;.&lt;br /&gt;
*** A list containing the string &amp;quot;bencode&amp;quot; and the integer -20 is encoded as &amp;lt;code&amp;gt;l7:bencodei-20ee&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Dictionaries&amp;#039;&amp;#039;&amp;#039; are encoded as &amp;lt;code&amp;gt;d&amp;lt;pairs&amp;gt;e&amp;lt;/code&amp;gt;.&lt;br /&gt;
** Begins with &amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt; and ends with &amp;lt;code&amp;gt;e&amp;lt;/code&amp;gt;.&lt;br /&gt;
** Contains key-value pairs.&lt;br /&gt;
** Keys are byte strings and must appear in lexicographical order.&lt;br /&gt;
** Each key is immediately followed by its value, which can be any bencoded type.&lt;br /&gt;
** Examples:&lt;br /&gt;
*** An empty dictionary is encoded as &amp;lt;code&amp;gt;de&amp;lt;/code&amp;gt;.&lt;br /&gt;
*** A dictionary with keys &amp;quot;wiki&amp;quot; → &amp;quot;bencode&amp;quot; and &amp;quot;meaning&amp;quot; → 42 is encoded as &amp;lt;code&amp;gt;d7:meaningi42e4:wiki7:bencodee&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Bencode defines only byte string types, rather than any particular [[character 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.&lt;br /&gt;
&lt;br /&gt;
=== Types of errors in Bencode ===&lt;br /&gt;
&lt;br /&gt;
Here is the list of the possible errors that a ill-formatted bencode may have:&lt;br /&gt;
&lt;br /&gt;
# Null root value.&lt;br /&gt;
# Non-singular root item.&lt;br /&gt;
# Invalid type encountered (character not &amp;#039;i&amp;#039;, &amp;#039;l&amp;#039;, &amp;#039;d&amp;#039;, or &amp;#039;0&amp;#039;-&amp;#039;9&amp;#039;).&lt;br /&gt;
# Missing &amp;#039;e&amp;#039; terminator for &amp;#039;i&amp;#039;, &amp;#039;l&amp;#039;, or &amp;#039;d&amp;#039; types.&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Integer errors:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
## Contains non-digit characters.&lt;br /&gt;
## Has a leading zero.&lt;br /&gt;
## Is negative zero.&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Byte string errors:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
## Negative length.&lt;br /&gt;
## Length not followed by &amp;#039;:&amp;#039;.&lt;br /&gt;
## Unexpected EOF before completing string.&lt;br /&gt;
## Length specified in units of codepoints (characters) rather than bytes.&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Dictionary errors:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
## Key is not a string.&lt;br /&gt;
## Duplicate keys.&lt;br /&gt;
## Keys not sorted.&lt;br /&gt;
## Keys incorrectly sorted by codepoint in a particular character encoding, rather than lexicographically sorted by ordinal.&lt;br /&gt;
## Missing value for a key.&lt;br /&gt;
&lt;br /&gt;
==Features==&lt;br /&gt;
Bencode is a very specialized kind of binary coding with some unique properties:&lt;br /&gt;
&lt;br /&gt;
* For each possible (complex) value, there is only a single valid bencoding; i.e. there is a [[bijection]] between values and their encodings. This has the advantage that applications may compare bencoded values by comparing their encoded forms, eliminating the need to decode the values.&lt;br /&gt;
* Bencoding serves similar purposes as data languages like [[JSON]] and [[YAML]], allowing complex yet loosely structured data to be stored in a [[platform independent]] way. This allowing a linear memory storage for complex data.&lt;br /&gt;
&lt;br /&gt;
== Drawbacks ==&lt;br /&gt;
&lt;br /&gt;
Bencode is not considered a [[human-readable]] encoding format. While the BE codegroups can be decoded manually, the bencoded values often contain [[Binary file|binary 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 a [[hex editor]] or specialised bencode editor tool must be used.&lt;br /&gt;
&lt;br /&gt;
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 where [[random access]] to fields is required.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
*[[Canonical S-expressions]]&lt;br /&gt;
*[[BitTorrent (protocol)|BitTorrent]]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
{{Reflist}}&lt;br /&gt;
&lt;br /&gt;
==External links==&lt;br /&gt;
* [https://wiki.theory.org/BitTorrentSpecification#Bencoding Bencoding specification]&lt;br /&gt;
* [http://pear.php.net/package/File_Bittorrent2/ File_Bittorrent2] - Another PHP Bencode/decode implementation&lt;br /&gt;
* [https://pypi.python.org/pypi/BitTorrent-bencode/5.0.8 The original BitTorrent implementation in Python as standalone package]&lt;br /&gt;
* [https://torrent-file-editor.github.io/ Torrent File Editor] cross-platform GUI editor for BEncode files&lt;br /&gt;
* [http://zakalwe.fi/~shd/foss/bencode-tools/ bencode-tools] - a C library for manipulating bencoded data and a XML schema like validator for bencode messages in Python&lt;br /&gt;
* [https://github.com/folz/bento Bento] - Bencode library in Elixir.&lt;br /&gt;
* [https://github.com/soulaway/beecoder Beecoder] - the file stream parser that de/encoding &amp;quot;B-encode&amp;quot; data format on Java using java.io.* stream Api.&lt;br /&gt;
* [https://web.archive.org/web/20131203085012/http://www.codecommit.com/blog/java/bencode-stream-parsing-in-java Bencode parsing in Java]&lt;br /&gt;
* [https://bitbucket.org/cs0ip/scala-lib_cs0ip.bencode Bencode library in Scala]&lt;br /&gt;
* [https://web.archive.org/web/20110724074506/http://funzix.git.sourceforge.net/git/gitweb.cgi?p=funzix%2Ffunzix%3Ba%3Dblob%3Bf%3Dbencode%2Fbencode.c Bencode parsing in C]&lt;br /&gt;
* [https://metacpan.org/search?q=bencode There are numerous Perl implementations on CPAN]&lt;br /&gt;
&lt;br /&gt;
[[Category:BitTorrent]]&lt;br /&gt;
[[Category:Data serialization formats]]&lt;/div&gt;</summary>
		<author><name>imported&gt;Thüringer</name></author>
	</entry>
</feed>