[util] implement bencode()

This commit is contained in:
Mike Fährmann
2018-03-14 13:17:34 +01:00
parent 4d74749496
commit b69cc94f0e
2 changed files with 25 additions and 0 deletions

View File

@@ -75,6 +75,16 @@ def optimize_range(ranges):
return result
def bencode(num, alphabet="0123456789"):
"""Encode an integer into a base-N encoded string"""
data = ""
base = len(alphabet)
while num:
num, remainder = divmod(num, base)
data = alphabet[remainder] + data
return data
def bdecode(data, alphabet="0123456789"):
"""Decode a base-N encoded string ( N = len(alphabet) )"""
num = 0