Numbersystem converter
This commit is contained in:
parent
03009f7824
commit
ee8728f2d7
1 changed files with 29 additions and 0 deletions
29
convert.py
Normal file
29
convert.py
Normal 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)
|
||||||
|
|
Loading…
Reference in a new issue