Created
April 3, 2014 08:07
-
-
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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