You can use built-in functions to convert integers to other bases' digit strings:
print( oct(64), hex(64), bin(64) ) # Numbers=>digit strings
The oct function converts decimal to octal, hex to hexadecimal, and bin to binary.
int function converts a string of digits to an integer.
Its an optional second argument specifies the numeric base:
print( 64, 0o100, 0x40, 0b1000000 ) # Digits=>numbers in scripts and strings print( int('64'), int('100', 8), int('40', 16), int('1000000', 2) ) print( int('0x40', 16), int('0b1000000', 2) ) # Literal forms supported too # from w w w.j av a2 s . co m
The eval function treats strings as though they were Python code.
print( eval('64'), eval('0o100'), eval('0x40'), eval('0b1000000') )
You can convert integers to base-specific strings with string formatting method calls and expressions, which return just digits, not Python literal strings:
print( '{0:o}, {1:x}, {2:b}'.format(64, 64, 64) ) # Numbers=>digits, 2.6+ print( '%o, %x, %x, %X' % (64, 64, 255, 255) ) # Similar, in all Pythons # www . j ava 2s. c om X = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFF print( X ) print( oct(X) ) print( bin(X) )