Skip to content

Instantly share code, notes, and snippets.

@mgd020
Created October 29, 2019 02:23
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 mgd020/2903534cce544f1c90db0cdaba4ef5e6 to your computer and use it in GitHub Desktop.
Save mgd020/2903534cce544f1c90db0cdaba4ef5e6 to your computer and use it in GitHub Desktop.
def base26_encode(n, max_len=6):
# base-26 alphabet (A-Z)
value = ''
digits = n
while digits > 0:
if len(value) == max_len:
raise ValueError(n)
value += chr((digits % 26) + A)
digits /= 26
value += 'A' * max(max_len - len(value), 0)
return value[::-1]
def base26_decode(string):
# base-26 alphabet
value = 0
for digit in string:
digit = (ord(digit) - A)
if digit < 0 or digit > 26:
raise ValueError(string)
value = value * 26 + digit
return value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment