Skip to content

Instantly share code, notes, and snippets.

@ethanal
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ethanal/5ef3c7f42c69ffd47b37 to your computer and use it in GitHub Desktop.
Save ethanal/5ef3c7f42c69ffd47b37 to your computer and use it in GitHub Desktop.
Base conversion with arbitrary alphabets
def baseconv(v1, a1, a2):
if v1 == a1[0]: return a2[0]
v2 = ""
n1 = {c: i for i, c in enumerate(a1)}
b1 = len(a1)
b2 = len(a2)
d1 = 0
for i, c in enumerate(v1):
d1 += n1[c] * pow(b1, len(v1) - i - 1)
while d1:
v2 = a2[d1 % b2] + v2
d1 //= b2
return v2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment