This is a way of serializing the data or representing data in torrent, ex; we use JSON while dealing with APIs, and BSON while dealing with mongodb.

why torrent chose bencoding over json:

  • json does not support binary format
  • bloated
  • mixed/varying key:value pairs

These above following drawbacks made the creators of torrent to chose bencoding over JSON in 2001, Bencoding is simple, deterministic, supports binary and extremely simple.

Bencoding supports ONLY 4 datatypes:

datatyperepresentationexampleintrepretation
Integeri<number>ei-45e, i0e, i99e45, 0, 99
Stringlength:string5:hello, 0:, 3:foo“hello”, “”, “foo”
Listl<item1><item2>...eli32e5:hello3:bye[32, "hello","bye"]
Dictionaryd<key><value>...ed3:key4:valuee{"key":"value"}lexicographically sorted by keys

Bencoding is recursive in nature, which means we can nest list inside dictionary and vice versa infinitely. ex:

d
  4:name
  8:file.txt
  6:length
  i123e
  5:files
  l
    d3:foo3:bare
  e
e

// actual structure:
{
	"name"   : "file.txt",
	"length" : 123,
	"files"  : [{"foo" : "bar"}]
}