Bencode Encoder/Decoder

BitTorrent's bencode: strings, integers, lists, dicts

Encode JSON into BitTorrent's bencode wire format and decode bencoded data back to JSON. Supports byte strings, integers, lists, and lexicographically-keyed dictionaries per the BEP-3 spec. Runs in your browser. It runs free in your browser on Gera Tools, with nothing uploaded.

Last updated Source: Gera Tools

What is bencode?

Bencode is the simple binary-safe serialization format used by BitTorrent for .torrent files and tracker responses. It encodes four types: byte strings, integers, lists, and dictionaries, each with an unambiguous prefix so the data is self-delimiting.

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:

  1. Byte string — the byte length, a colon, then the data: 4:spam.
  2. Integeri, the base-10 number, then e: i42e (negatives allowed, no leading zeros).
  3. Listl, each encoded element, then e: l4:spam4:eggse.
  4. Dictionaryd, alternating encoded string-key/value pairs, then e. 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 0 must be encoded as i0e. Leading zeros are forbidden: i01e is 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.