Skip to content

Instantly share code, notes, and snippets.

@btbytes
Created September 3, 2015 21:05
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 btbytes/2a88b2098c75b62b0893 to your computer and use it in GitHub Desktop.
Save btbytes/2a88b2098c75b62b0893 to your computer and use it in GitHub Desktop.
def base36encode(number, alphabet='23456789ABCDEFGHJKMNPQRSTUVWXYZ'):
"""Converts an integer to a base36 string."""
if not isinstance(number, (int, long)):
raise TypeError('number must be an integer')
base36 = ''
sign = ''
if number < 0:
sign = '-'
number = -number
if 0 <= number < len(alphabet):
return sign + alphabet[number]
while number != 0:
number, i = divmod(number, len(alphabet))
base36 = alphabet[i] + base36
return sign + base36
def base36decode(number):
return int(number, 36)
for i in range(37, 1000):
print base36encode(i).lower(), ' ',
@btbytes
Copy link
Author

btbytes commented Sep 3, 2015

encode/decode source: http://stackoverflow.com/a/1181922

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment