Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/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
You can’t perform that action at this time.