Skip to content

Instantly share code, notes, and snippets.

@toshsan
Created November 11, 2014 11:49
Show Gist options
  • Save toshsan/ded6afc44fd916e2ccb8 to your computer and use it in GitHub Desktop.
Save toshsan/ded6afc44fd916e2ccb8 to your computer and use it in GitHub Desktop.
BASE_ALPH = tuple("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
BASE_DICT = dict((c, v) for v, c in enumerate(BASE_ALPH))
BASE_LEN = len(BASE_ALPH)
def base_decode(string):
num = 0
for char in string:
num = num * BASE_LEN + BASE_DICT[char]
return num
def base_encode(num):
if not num:
return BASE_ALPH[0]
encoding = ""
while num:
num, rem = divmod(num, BASE_LEN)
encoding = BASE_ALPH[rem] + encoding
return encoding
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment