Representation of each hexadecimal data in python
a = 23 # 默认的数字都是表示十进制
print(a)
a = 0b10111 # 以0b开头的数字表示二进制
print(a)
a = 0o27 # 以0o开头的数字表示八进制
print(a)
a = 0x17 # 以0x开头的数字表示十六进制
print(a)
The data printed out by default is decimal.
You can use the English letter "box" for memory
Conversion between bases
Convert decimal to binary
When we get a decimal number, we divide it by 2 until the quotient is 0. Reverse the remainder obtained in the process to get the binary representation.
It is abbreviated as "divide by 2 in the forward direction and take the remainder in the reverse direction".
For example: decimal number 23
Convert binary to decimal
Multiply 1 2 4 8 16... with the binary bits corresponding to the n-1 power of 2, taking 10111 as an example:
Binary to octal
Divide the three bits of the binary into one group, fill in the missing bits with 0, and each group converts the binary to decimal according to the method, and finally write it out in order to get the octal data. For example: 10111
Convert binary to hexadecimal
The principle is the same as the octal system, the binary is divided into four digits into a group, the missing digits are filled with 0, and then written in sequence according to the method of converting the binary to the decimal system, such as 10111
Bit conversion with code
The above is the principle of hexadecimal conversion. It is too tiring to calculate by hand. This process can be implemented in python with code.
a = 23
print(bin(a)) # 转化为二进制
print(oct(a)) # 转化为八进制
print(hex(a)) # 转化为十六进制