Bencode Decoder
Decode bencode, the format of .torrent files and BitTorrent messages, into readable JSON. Runs in your browser.
Input
Output
Guide
How do you read bencode?
Bencode is the format of .torrent files and BitTorrent messages. Each value starts with a mark that tells you what it is:
istarts a whole number, which ends withe, as ini42e- A number and a colon start a string of that many bytes, as in
6:tulips lstarts a list, which ends withe, as inl6:tulips7:daisiesedstarts a dictionary of keys and values, which ends withe, as ind4:name12:Sunny Gardene
There are no spaces between values, so bencode reads as one long line. Most bencode you'll meet is a single dictionary that starts with d, and the contents of a .torrent file often begin with d8:announce.
Tool description
Paste bencode into the first editor, and it appears as indented JSON in the second one as you type, ready to read or copy. Dictionaries become objects, lists become arrays, and strings become text. Text is read as UTF-8, so string lengths count bytes, as in bencode written by any program: 6:crème is right, since è takes two bytes. Spaces and line breaks before and after the bencode are ignored. Insert sample fills in the details of a garden.
To turn JSON into bencode, press the swap button between the editors, which also moves the result into the input, or use the Bencode Encoder.
Features
- Decodes bencode to indented JSON as you type
- Errors that name the problem and the character where it is
- Runs in your browser
Examples
| Bencode | JSON |
|---|---|
i-12e |
-12 |
6:crème |
"crème" |
l5:bread6:cheese6:grapese |
["bread", "cheese", "grapes"] |
d4:cake10:Lemon tart6:slicesi8ee |
{"cake": "Lemon tart", "slices": 8} |
The tool writes the JSON indented over several lines; here it's shown on one.
Fixing errors
- "The bencode ends too soon" means a list or dictionary is missing its closing
e, as inl6:tulips7:daisies, or a string is shorter than its length says, so the string takes in what follows. - "The bencode value is complete, but more text follows" means there's more than one value, as in
6:tulips7:daisies. Put them in a list:l6:tulips7:daisiese. - "A string's length doesn't match its text" means a length counts letters where it should count bytes:
4:cafégives this error, and5:caféis right. - "This isn't valid bencode" names the character where no value can start, such as plain text, a space between two items of a list or a dictionary key that isn't a string. A length that's too small causes it too: in
d4:name11:Sunny Gardene, the string stops one letter early, and thenleft over at character 22 can't start a value. - "A number is too large to convert without losing digits" appears for whole numbers too large for JavaScript to hold exactly.
Good to know
- Bencode strings can hold any bytes, and .torrent files keep binary data in them, such as the hashes of their pieces. Pasted text can't carry such bytes, so to look inside a .torrent file, use the Torrent File Reader.