To achieve the following conversion through code?

Binary to decimal conversion: v = "0b1111011"


The first

Is coupled with 0b, will automatically convert binary number is displayed before the decimal, note that this is not a string

x = 0b1010
print(x)


If the string can be evaluated using eval

x = eval('0b1010')

The second

Using int function strings may be prefixed 0b, it may not be used.

int('1010',base=2)
int('0b1010',2)
K = int ( '1111011', the base = 2) 
Print (right) 

ret1 = int ( '0b1111011', 2) 
printing (ret1)

 

Convert decimal to binary: v = 18
 

K = bin (18 )
 the printer (right)

 

 


Octal to decimal: v = "011"
 

1、

K = eval (str (bin (0o11)))
 print (right)

 

 

2、

K = int ( ' 011 ' , 8 )
 the printer (right)

 

 

 

Decimal to octal: v = 30
 

oct K = (18 )
 the printer (right)

 

 


Hexadecimal to decimal conversion: v = "0x12"
 

K = int ( ' 0x12 ' , 16 )
 the printer (right)

 

 

 

 

 

Convert decimal to hexadecimal: v = 87

K = hex (87 )
 the printer (right)

Guess you like

Origin www.cnblogs.com/Rivend/p/12038591.html