Last active
August 29, 2015 14:02
-
-
Save ethanal/5ef3c7f42c69ffd47b37 to your computer and use it in GitHub Desktop.
Base conversion with arbitrary alphabets
This file contains 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
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