Sep-29-2020, 06:46 PM
This one creates strings suitable for putting back into
int(x, base=36)
. Not as useful for arbitrarily large bases.import string import random base = 36 mapping = string.digits + string.ascii_uppercase def int_to_other_base(num, base): ans = "" while num > 0: num, remainder = divmod(num, base) ans = mapping[remainder] + ans return ans num = random.randint(0,1000000000000000) print(f"For integer {num}") rebased = int_to_other_base(num,base) print(f"In base {base}: {rebased}") print(f"and converted back to an int: {int(rebased,base)}")
Output:For integer 19520920808903
In base 36: 6X3SC8FRB
and converted back to an int: 19520920808903