I'm wanting to write a program that asks the user to enter a number in base 10, and to enter the base that they want to convert to. What is the highest base that I can convert to without having my program become incredibly complicated? I'm thinking Base 9, because after 10 (which is already given), the bases start to use letters. Am I correct?
Note: By complicated, I mean complicated for a Python beginner. I'm very far off from being skilled with the program.
Note 2: I'm attempting to do this without mod开发者_运维百科ules.
If you put the digit values in a string then the base you can use gets very high very easily.
radixdigits = '0123456789ABCDEFGHI...'
And then you can do divmod()
with the length of the string in order to get the current digit, and index the string with that number.
What is the highest base that I can convert to without having my program become incredibly complicated?
In ASCII, base 127. However, that will be hard to read because of the upper-case/lower-case things looking similar, and the non-printable ASCII characters requiring a lot of complex escapes.
You're better limiting things to about base 36 because you can use digits and letters without any confusing ambiguity.
In Unicde, base 65536 or some such, depending on how many Unicode characters you want to deal with.
I'm thinking Base 9, because after 10 (which is already given), the bases start to use letters. Am I correct?
You're sort-of correct.
Standard ASCII has 9 digit symbols. You're not forced to use letters, but it's pretty common to use letters.
You could use lots of Unicode symbols that are neither letters nor digits.
I'm attempting to do this without modules.
That's not terribly relevant.
Now read other Stackoverflow questions on Base Conversion.
Here's a starter: Base 62 conversion
Here's the search: https://stackoverflow.com/search?q=%5Bpython%5D+base+conversion
精彩评论