Skip to content

Instantly share code, notes, and snippets.

@joshmaker
Created April 14, 2014 17:52
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 joshmaker/10669230 to your computer and use it in GitHub Desktop.
Save joshmaker/10669230 to your computer and use it in GitHub Desktop.
Simple python utility function for performing base conversions
import string
corpus = string.digits + string.letters
def base_convert(num, base):
if base > len(corpus) or base < 2:
raise Exception('Please pick a base between 2 and %s' % len(corpus))
quotient, remainder = divmod(abs(num), base)
return "".join([
'-' if num < 0 else '',
base_convert(quotient, base) if quotient else '',
corpus[remainder],
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment