diff --git a/convert.py b/convert.py new file mode 100644 index 0000000..2edd281 --- /dev/null +++ b/convert.py @@ -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) +