one-file-projects/convert.py

30 lines
554 B
Python

def convert(input, source, target):
sbase = len(source)
tbase = len(target)
value = 0
for i,c in enumerate(input):
p = len(input)-i-1
b = source.index(c)
value += b * sbase ** p
print(value)
if value == 0:
return target[0]
output = ""
while value > 0:
b = target[value % tbase]
output = b + output
value = value // tbase
print("%s -> %s" % (input,output))
return output
dec = "0123456789"
hex = dec + "ABCDEF"
bin = "01"
convert("15",dec,bin)