Skip to content

Instantly share code, notes, and snippets.

@apalala
Created January 29, 2014 14:43
Show Gist options
  • Save apalala/8689371 to your computer and use it in GitHub Desktop.
Save apalala/8689371 to your computer and use it in GitHub Desktop.
Convert the given integer to the base and symbols of the given alphabet
import string
ALPHABET = string.digits + string.ascii_letters
def rebase(value, width=1, alphabet=ALPHABET):
""" Convert the given integer to the base
and symbols of the given alphabet
"""
n = len(alphabet)
result = []
while value != 0:
c = value % n
result.append(alphabet[c])
value //= n
np = width - len(result)
if np <= 0:
padding = []
else:
padding = [alphabet[0]] * np
sresult = ''.join(padding + result)
return sresult
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment