Binary conversion in 06 python language
Binary conversion in python language
1. Which binary systems are supported in the python language and their representations
2. How to convert between different binary systems
/ / py language supports 4 binary systems
Decimal 0-9 / 123 defaults to decimal
Hexadecimal 0-9 Amurf / / 0x 0xFF12E 0x begins with hexadecimal
Binary / / 0b 0b11010101 / / it's OK to use 0B here, but in the industry, the lowercase 0b is used for binary.
Octal / 0o 0o4324 / / 0o begins with octal
Except for decimal, all other decimal numbers are preceded by 0
The conversion is mainly from decimal to binary 86 and then from binary 86 to decimal.
/ / to convert from other decimal to decimal, use the int function, which takes two parameters
Parameter 1: the string type represents the number to be converted
Parameter 2: numeric typ
Example # coding:utf-8print (int ("0b11010101", 2)) # 213 / / this means that the number converted from binary 11010101 to decimal is print (int ("0o4324", 8)) # 2260 octal to print (int ("0xFF12E", 16)) # 1044782 hexadecimal conversion to the present We are going to reverse the conversion from decimal to binary 86 for example, to convert 432 to binary 86 # coding:utf-8print (bin (432)) # this is converted to binary print (oct (432)) # this is converted to octal print (hex (432)) # This is converted to hexadecimal output to 0b1101100000o6600x1b0, or you can directly convert hexadecimal to binary print (bin (0xFF12E)) output result 0b11111111000100101110print (hex (0o4324)) convert octal to hexadecimal output 0x8d4print (0b110101) / / if you write it this way, the output is decimal 53print (0x123FE) print (0o33333) 7475014043