image

the sleepy snake

index :: num :: to_base

to_base(to_base, num, base=10)

onverts a number or string to a number of the given base

to_base integer, base to convert to (must be base 2-36 or base 256)
num integer/string, number to convert
base integer, base of the number passed (must be base 2-36 or base 256)

Retuns: the resulting number as string

Note: The function handles hexadecimal strings prefixed with HEX_PREFIXES as defined in the module aswell, as long as the correct base (16) is specified

# convert string containng hex number to binary
bin = to_base(2, '0xff', base=16)
>> '11111111'

# convert from any base to bytes and back again
integer = 255
bytes = to_base(256, integer, base=10)
print bytes 
>> ''\xff''
bits = to_base(2, bytes, base=255)
print bits
>> '11111111'