Skip to content

Instantly share code, notes, and snippets.

@eirenik0
Created April 3, 2014 08:07
Show Gist options
  • Save eirenik0/9950327 to your computer and use it in GitHub Desktop.
Save eirenik0/9950327 to your computer and use it in GitHub Desktop.
The algorithm for binary conversion can easily be extended to perform the conversion for any base. In computer science it is common to use a number of different encodings. The most common of these are binary, octal (base 8), and hexadecimal (base 16).
def baseConverter(decNumber,base):
digits = "0123456789ABCDEF"
remstack = Stack()
while decNumber > 0:
rem = decNumber % base
remstack.push(rem)
decNumber = decNumber // base
newString = ""
while not remstack.isEmpty():
newString = newString + digits[remstack.pop()]
return newString
print(baseConverter(25,2))
print(baseConverter(25,16))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment