Bencode (pronounced “bee-encode”) is the minimal serialization format that powers BitTorrent — every .torrent file and tracker reply is bencoded. It supports just four types, each self-delimiting. This tool converts JSON to bencode and back, in your browser.
How it works
Bencode encodes four types with distinct prefixes:
- Byte string — the byte length, a colon, then the data:
4:spam. - Integer —
i, the base-10 number, thene:i42e(negatives allowed, no leading zeros). - List —
l, each encoded element, thene:l4:spam4:eggse. - Dictionary —
d, alternating encoded string-key/value pairs, thene. Keys must be sorted in raw byte order so each value has exactly one canonical encoding.
Decoding reverses this by reading the leading character to pick the type, then consuming exactly the bytes the prefix describes.
Complete format reference
Type | Format | Example input | Encoded output
------------|---------------------|---------------|------------------
String | <len>:<bytes> | "spam" | 4:spam
Integer | i<n>e | 42 | i42e
| negative: i-42e | -42 | i-42e
| zero: i0e | 0 | i0e
List | l<items>e | ["a", 1] | l1:ai1ee
Dictionary | d<k/v pairs>e | {"a": 1} | d1:ai1ee
Key constraints:
- Integer
0must be encoded asi0e. Leading zeros are forbidden:i01eis invalid. - String lengths are always in decimal bytes, not characters (important for multi-byte UTF-8).
- Dictionary keys must always be byte strings, never integers or nested structures.
- Dictionary keys must be sorted in ascending raw byte order (lexicographic by bytes, not by Unicode code point when keys contain non-ASCII bytes).
Worked examples
Simple dictionary:
JSON: {"name": "Alice", "age": 30}
Bencode: d3:agei30e4:name5:Alicee
Notice that keys are sorted: age (ASCII a) comes before name (ASCII n). The JSON input order does not matter — the encoder always outputs sorted keys.
Nested structure (like a torrent info dict):
JSON: {"files": ["a.txt", "b.txt"], "piece length": 262144}
Bencode: d5:filesl5:a.txt5:b.txte12:piece lengthi262144ee
Why this matters for torrents: The info dictionary inside a .torrent file is bencoded and then SHA-1 (or SHA-256 for BitTorrent v2) hashed to produce the info hash — the unique identifier that trackers and peers use to identify the torrent. Because bencode mandates sorted dictionary keys, any two correct encoders will produce identical bytes for the same data, guaranteeing identical hashes.
What bencode cannot represent
Bencode is intentionally minimal. These JSON types have no bencode equivalent:
- Floats (
3.14,NaN,Infinity) — no float type exists - Booleans (
true,false) — must be encoded as integers (1/0) by convention if needed - Null — no null type; omit the key or use an empty string
Attempting to encode these will produce an error in this tool.