Numbersystem converter

This commit is contained in:
madmaurice 2016-03-17 11:55:27 +01:00
parent 03009f7824
commit ee8728f2d7

29
convert.py Normal file
View file

@ -0,0 +1,29 @@
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)