Python has the capability to convert a base 10 integer to base X where 2<=X<=36, with the int()
function. It would seem natural (until you've studied group theory) that there would be a way to convert numbers back to base 10, or from base X to base Y for any integer.
I could write a function using from math import log
to convert between bases, but I was wondering if there is anything that is already built in to python to accomplish this.
Is there a built in function in Python 3, to perform a change of base between any two bases? What constraints, if any, are there similar to those for int()
?
Edit: To clarify, int()
only interprets the first argument in base 10. I would like to specify the argument as a different base representation. I am wondering if there is a built in function that takes two arguments for base representation.
Example (why int()
is not the answer:
>>> int('100',9) == int(str(int('81',9)),10)
False
Edit #2: Say I want to know what 100 is in base 9, I would use the following, int('100', 9) this would return a value of 81. Now say I want to take 81 (in base 9) and turn it back into base 10. Is there a function built into Python to accomplish this, or a way to mark the first argument to indicate that I am giving it a number in base 9?
I know that there is a way to specify this for binary, using 'ob'
, and hex values using '0x'
. For example,
int('0b1100100', 2)
This return a value of 100, what are the prefixes to specify other bases for the first argument.