Bencode to XML Converter
Convert Bencode data to well-formed XML. Parse .torrent files, tracker responses, and DHT data with proper binary handling and formatting.
XML output will appear here...
About
Bencode (pronounced bee-encode) is the encoding format used by BitTorrent for storing and transmitting loosely structured data. The format supports four data types: byte strings (length-prefixed), integers (signed, base-10), lists (ordered sequences), and dictionaries (key-value maps with sorted string keys). Unlike JSON or XML, Bencode lacks native support for floating-point numbers, null values, or boolean types. The format's simplicity makes it resistant to parsing ambiguities but creates challenges when converting to richer formats. This converter implements a full recursive descent parser per the BitTorrent Enhancement Proposal specifications, handling edge cases like nested structures exceeding 100 levels deep and binary data (piece hashes, peer IDs) that require Base64 encoding for valid XML output.
Conversion errors typically occur with malformed length prefixes (string declares 500 bytes but contains 50), unclosed containers (missing e terminator), or invalid dictionary key ordering. The parser performs strict validation: dictionaries with out-of-order keys are flagged as non-canonical Bencode. Binary strings containing non-UTF8 sequences are automatically Base64-encoded with an encoding="base64" attribute to preserve round-trip fidelity.
Formulas
Bencode parsing follows a deterministic grammar where each type is identified by its leading byte. The parser implements a recursive descent strategy with lookahead of 1 character.
Type detection occurs via the first byte:
String length prefix parsing extracts n bytes after the colon delimiter:
XML escaping applies five mandatory character substitutions to ensure well-formed output:
Binary detection uses UTF-8 validation. If a byte sequence fails UTF-8 decoding, the string is Base64-encoded:
Reference Data
| Bencode Type | Syntax Pattern | XML Mapping | Example Bencode | Example XML Output |
|---|---|---|---|---|
| Integer | i<number>e | <integer> | i42e | <integer>42</integer> |
| Negative Integer | i-<number>e | <integer> | i-17e | <integer>-17</integer> |
| Zero | i0e | <integer> | i0e | <integer>0</integer> |
| String (ASCII) | <len>:<data> | <string> | 4:spam | <string>spam</string> |
| String (UTF-8) | <len>:<data> | <string> | 6:日本語 | <string>日本語</string> |
| String (Binary) | <len>:<bytes> | <string encoding="base64"> | 20:<binary SHA1> | <string encoding="base64">...</string> |
| Empty String | 0: | <string> | 0: | <string></string> |
| List (Empty) | le | <list> | le | <list></list> |
| List (Items) | l<items>e | <list><item>...</item></list> | li1ei2ee | <list><item><integer>1</integer></item><item><integer>2</integer></item></list> |
| Dictionary (Empty) | de | <dict> | de | <dict></dict> |
| Dictionary (Entries) | d<key><value>e | <dict><key name="...">...</key></dict> | d3:foo3:bare | <dict><key name="foo"><string>bar</string></key></dict> |
| Nested Structure | Any combination | Recursive mapping | d4:infod4:name4:testee | <dict><key name="info"><dict><key name="name"><string>test</string></key></dict></key></dict> |
| Torrent Announce | URL string | <string> | d8:announce38:http://tracker.example.com:6969/announcee | <dict><key name="announce"><string>http://...</string></key></dict> |
| Piece Length | Power of 2 integer | <integer> | d12:piece lengthi262144ee | <key name="piece length"><integer>262144</integer></key> |
| Info Hash Data | 20-byte binary | Base64 encoded | 6:pieces20:<SHA1 bytes> | <key name="pieces"><string encoding="base64">...</string></key> |
| Creation Date | Unix timestamp | <integer> | d13:creation datei1234567890ee | <key name="creation date"><integer>1234567890</integer></key> |
| File List | List of dicts | Nested structure | d5:filesld6:lengthi1024e4:pathl8:file.txteee | <key name="files"><list><item><dict>...</dict></item></list></key> |
| Private Flag | Integer 0 or 1 | <integer> | d7:privatei1ee | <key name="private"><integer>1</integer></key> |
| Comment | UTF-8 string | <string> | d7:comment12:Hello World!e | <key name="comment"><string>Hello World!</string></key> |
| URL List | String or List | Depends on type | d8:url-listl20:http://mirror1.com/20:http://mirror2.com/ee | <key name="url-list"><list>...</list></key> |
| DHT Nodes | List of lists | Nested lists | d5:nodesll9:127.0.0.1i6881eeee | <key name="nodes"><list><item><list>...</list></item></list></key> |