The content of this article is a little bit difficult. If you are just a python enthusiast, you can ignore this tutorial. If you are a computer-related professional learner or practitioner, then I suggest you read it carefully.
The number we usually use is decimal, and 1 is entered every 10, while the computer uses binary. In addition, sometimes you will encounter octal and hexadecimal. In fact, as long as you master the method, it is not difficult to understand these bases. The conversion between each other is not complicated, let’s take a look at a decimal example
432
Of course you know that 4 is in the hundreds place, 3 is in the tens place, and 2 is in the ones place. This value can be described in another way.
4*102 + 3*101 + 1*100 = 400 + 30 + 1 This formula is introduced to give you a better understanding of how to convert other numbers into decimal
Take 1101010 as an example to explain how to convert to a decimal value
1101010 = 1*26 + 1*25 + 0*24 + 1*23 + 0*22 + 1*21 + 0*20 = 64 + 32 + 8 + 4 = 106 The binary value in python starts with 0b
print(0b1101010) # 106
If you have understood how to convert binary to decimal, then it becomes easy to convert octal to decimal. Take octal 125 as an example.
125 = 1*82 + 2*81 + 5*80 = 64 + 16 + 5 = 85 In python, the octal value starts with 0o
print(0o125) # 85
Hexadecimal is slightly more troublesome than octal. Hexadecimal is 1 in every hexadecimal system, so the value of a single digit will exceed 9, and the part exceeding 9 will be used in sequence a, b, c, d, e, f, respectively representing 10, 11, 12, 13, 14, 15.
Take hexadecimal 3f2a1 as an example. 3f2a1 = 3*164 + 15*163 + 2*162 + 10*161 + 1*160 = 196608 + 61440 + 512 + 160 + 1 = 258721
The hexadecimal value in python starts with 0x
print(0x3f2a1) # 258721
value = int('1101010', 2) print(value) # 106 value = int('125', 8) print(value) # 85 value = int('3f2a1', 16) print(value) # 258721
Use the int function to convert binary, octal, and hexadecimal values into decimal values, and the beginning of the string can not carry the hexadecimal identifier, if you like to use it, you can also write int('0x3f2a1', 16)
Use the bin() function to convert decimal to binary, oct() to convert decimal to octal, and hex() to convert decimal to hexadecimal
print(bin(106)) # 0b1101010 decimal to binary print(oct(85)) # 0o125 Decimal to octal print(hex(258721)) # 0x3f2a1 Decimal to hexadecimal
There is no function to directly convert an octal or hexadecimal number into binary, so you need to use the int() function to convert the octal number to decimal first, and then use the bin() function to convert the decimal number to binary
print(bin(int('125', 8))) # octal to binary print(bin(int('0x3f2a1', 16))) # hexadecimal to binary
Binary to octal can be converted directly using the oct function, while binary to hexadecimal requires the int function
print(oct(0b1101010)) # Binary to octal print(hex(int('1101010', 2))) # Binary to hexadecimal
Hexadecimal to octal, you can use the oct function directly, and octal to hexadecimal requires the int function
print(oct(0x3f2a1)) # hexadecimal to octal print(hex(int('125', 8))) # octal to hexadecimal
The most powerful is the oct function, which can directly convert the values of the other three bases into octal.