Bencode encoder and decoder for Julia
Bencode.jl
can be installed from the Julia REPL using the following command:
julia> ]
pkg> add https://github.com/guilemieux/Bencode.jl
using Bencode
data = Dict(
"string" => "Hello World",
"integer" => 12345,
"dict" => Dict(
"key" => "value"
),
"list" => [1, 2, "string", 3, Dict()]
)
String(bencode(data))
# "d4:dictd3:key5:valuee7:integeri12345e4:listli1ei2e6:stringi3edee6:string11:Hello Worlde"
typeof(bencode(data))
# Array{UInt8, 1}
data = bdecode("d4:dictd3:key5:valuee7:integeri12345e4:listli1ei2e6:stringi3edee6:string11:Hello Worlde")
bdecode(data)
Output:
Dict(
"dict" => Dict{Any,Any}(
"key" => "value"
),
"list" => Any[1, 2, "string", 3, Dict{Any,Any}()]
"string" => "Hello World",
"integer" => 12345
)
It is also possible to decode the bencoding without coverting the bytes to utf-8 strings.
bdecode(data, bytestostr=false)
Output:
Dict{Any,Any}(
"dict" => Dict{Any,Any}(
"key" => UInt8[0x76, 0x61, 0x6c, 0x75, 0x65]
),
"list" => Any[1, 2, UInt8[0x73, 0x74, 0x72, 0x69, 0x6e, 0x67], 3, Dict{Any,Any}()],
"string" => UInt8[0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64],
"integer" => 12345
)