Created
November 5, 2018 20:45
-
-
Save lgommans/a4dc3c37294962716820d8e4ac2fee1d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import string, math, sys | |
ibase = '0123456789' | |
ival = sys.argv[1] | |
obase = '0123456789abcdef' | |
obase = string.digits + string.ascii_lowercase | |
obase = string.printable[:94].replace(',', '') | |
def ceilup(n): | |
if int(n) == round(n) == math.ceil(n): | |
return n + 1 | |
return math.ceil(n) | |
original_ival = ival | |
obase_dec = len(obase) | |
val = 0 | |
while ival != '': | |
val += ibase.index(ival[0]) * (len(ibase) ** (len(ival) - 1)) | |
ival = ival[1:] | |
print('{} in base {} converts to {} in base 10'.format(original_ival, len(ibase), val)) | |
ostr = '' | |
for pos in range(int(ceilup(math.log(val)/math.log(len(obase)))), 0, -1): | |
tmp_dec = math.floor(val / (len(obase) ** (pos - 1))) | |
dec_worth = tmp_dec * (len(obase) ** (pos - 1)) | |
print('tmp_dec={} dec_worth={} pos={} val={}'.format(tmp_dec, dec_worth, pos, val)) | |
tmp_obase = obase[tmp_dec] | |
val -= dec_worth | |
ostr += tmp_obase | |
print(ostr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment