Bencode Encoder/Decoder

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

Ad placeholder (leaderboard)

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.

Tips and examples

The JSON object {"cow":"moo","spam":"eggs"} encodes to d3:cow3:moo4:spam4:eggse, with keys already in sorted order. The list ["spam", 42] becomes l4:spami42ee. Because bencode has no float or boolean type, encoding true or 3.14 is rejected. The sorted-key rule is what makes a torrent’s info-hash reproducible, so always trust the encoder’s ordering over your input order.

Ad placeholder (rectangle)