Convert between binary, octal, decimal, and hexadecimal

1. Conversion between decimal and binary
(1) Converting decimal to binary is divided into integer part and fractional part
① Integer part
Method : divide by 2 and take the remainder, that is, divide the integer part by 2 each time, and the remainder is the weight of the bit The quotient continues to be divided by 2, and the remainder is the number on the previous bit weight. This step continues until the quotient is 0. When the last reading is made, read from the last remainder until the first a remainder. The following example:
Example: Convert 168 in decimal to binary

and get the result Convert 168 in decimal to binary, (10101000) 2
Analysis: The first step is to divide 168 by 2, the quotient is 84, and the remainder is 0.
The second step is to divide the quotient 84 by 2, and the remainder of the quotient 42 is 0.
The third step is to divide the quotient 42 by 2, and the remainder of the quotient 21 is 0.
The fourth step is to divide the quotient 21 by 2, and the remainder of the quotient 10 is 1.
The fifth step is to divide the quotient 10 by 2, and the remainder of the quotient 5 is 0.
The sixth step is to divide quotient 5 by 2, and the remainder of quotient 2 is 1.
The seventh step is to divide quotient 2 by 2, and the remainder of quotient 1 is 0.
The eighth step is to divide the quotient 1 by 2, and the remainder of the quotient 0 is 1.
The ninth step, reading, because the last digit is obtained by dividing by 2 many times, so it is the highest digit, and the reading digit is read forward from the last remainder, that is, 10101000

(2) Fractional part
Method : multiply by 2 and round up The method is to multiply the fractional part by 2, then take the integer part, continue to multiply the remaining fractional part by 2, then take the integer part, and multiply the remaining fractional part by 2 until the fractional part is taken
until zero. If it can never be zero, it is the same as the rounding of decimal numbers. When the number of decimal places is reserved according to the requirements, it is rounded according to whether the next digit is 0 or 1. If it is zero, it is rounded. In other words, 0 is rounded to 1. The reading should be read from the previous integer to the following integer. The following example:
Example 1: Convert 0.125 to binary

to get the result: Convert 0.125 to binary (0.001) 2
Analysis: The first step, multiply 0.125 by 2 to get 0.25 , then the integer part is 0 and the fractional part is 0.25; in
the second step, multiply the fractional part 0.25 by 2 to get 0.5, then the integer part is 0 and the fractional part is 0.5; in
the third step, multiply the fractional part 0.5 by 2, If 1.0 is obtained, the integer part is 1 and the fractional part is 0.0;
the fourth step is to read from the first digit to the last digit, which is 0.001.


Example 2, convert 0.45 to binary (reserved to the fourth decimal place)


. As you can see from the above steps, when the fifth multiplication is performed, the result obtained is 0.4, then the decimal part continues to be multiplied by 2 to get 0.8, 0.8 Multiply by 2 again, and keep multiplying until 1.6. In the end, it is impossible to get the fractional part to be zero. Therefore, at this time, we have to learn the decimal method for rounding, but the binary only has two 0 and 1, so 0 is rounded to 1. enter. This is also the error that the computer will generate in the conversion, but because of the large number of reserved digits and high precision, it can be ignored.
Then, we can draw the result that converting 0.45 to binary is approximately equal to 0.0111
. The method described above is the method of converting decimal to binary. What everyone needs to pay attention to is:
1) To convert decimal to binary, it needs to be divided into two parts: integer and decimal. Conversion
2) When converting integers, use the remainder by dividing by 2, and when converting decimals, use multiplying by 2.
3) Pay attention to their reading direction
Therefore, from the above method, we can conclude that the decimal number 168.125 converted to binary is 10101000.001, or the decimal number converted to binary number is approximately equal to 10101000.0111.

(3) Convert binary to decimal without dividing integer and fractional parts
Method : Addition by weight, that is, multiply the number on each bit of the binary by the weight, and then add the sum to get the decimal number. Example
Convert the binary number 101.101 to decimal.

The result is: (101.101) 2=(5.625) 10
What you need to pay attention to when converting binary to decimal is
1) To know the weight of each bit of binary
2) To be able to find the value of each bit


2. between binary and octal
First of all , we need to understand a mathematical relationship, that is, 23=8, 24=16, and octal and hexadecimal are
derived from this relationship, that is, three bits are used to represent one bit of octal, and four bits are used to represent one bit. Binary represents a hexadecimal number.
Next, memorize the 4 numbers 8, 4, 2, 1 (23=8, 22=4, 21=2, 20=1). Now let's practice converting between binary and octal.
(1) Convert binary to octal
Method : take the three-in-one method, that is, from the decimal point of the binary as the dividing point, take every three bits to the left (right) into one bit, and then add the three binary bits according to the weight to get The number is an eight-digit binary number, and then, arranged in order, the position of the decimal point remains unchanged, and the obtained number is the octal number we seek. If you take three digits to the left (right), when you get the highest (lowest) digit, if you can't make up three digits, you can add 0 to the leftmost (rightmost) decimal point, that is, the highest (lowest) digit of the integer. Get three. Example
① Convert the binary number 101110.101 to octal

to get the result: Convert 101110.101 to octal to 56.5

② Convert the binary number 1101.1 to octal

to get the result: Convert 1101.1 to octal to 15.4

(2) Convert octal to binary
Method : Take one-to-three method, that is, decompose an octal number into three-bit binary numbers, and use three-bit binary numbers Add up to make up the octal number by weight, and the decimal point position remains the same. Example:
① Convert the octal number 67.54 to binary.

Therefore , converting the octal number 67.54 to binary is 110111.101100, that is, 110111.1011. As
you can see from the above question, the calculation of octal to binary conversion
First , convert the octal number from left to right, Expand each bit into three digits, and the decimal point position remains unchanged.
Then , expand each bit into 22, 21, and 20 (ie, 4, 2, and 1) to make up the number, that is, a×22+ b×21 +c×20= The number on the bit (a=1 or a=0, b=1 or b=0, c=1 or c=0), the abc arrangement is the binary number of the bit
Then , convert each bit into a binary number Arrange in order
Finally , you get the octal-to-binary number.
The above method is the exchange of binary and octal. What you need to pay attention to when doing the question is
1) The exchange between them is a one-bit and three-bit conversion, which is different from the binary and decimal conversion
. 2) Everyone is in When adding 0 and removing 0, pay attention to adding 0 or removing 0 only at the leftmost or the rightmost of the decimal point (that is, the highest digit of the integer and the lowest digit of the decimal), otherwise an error will occur.

3. Binary and sixteen Hexadecimal conversion
method : similar to binary and octal conversion, except that it is the conversion of one bit (
hexadecimal ) and four bits (binary).
Method: Take the four-in-one method, that is, from the decimal point of the binary as the dividing point, take every four digits to the left (right) into one digit, and then add the four binary digits according to the weight, and the obtained number is one 16 digits The binary numbers are then arranged in order, the position of the decimal point remains unchanged, and the obtained number is the hexadecimal number we are looking for. If you take four digits to the left (right), when you get the highest (lowest) digit, if you cannot make up all four digits, you can add 0 to the leftmost (rightmost) decimal point, that is, the highest (lowest) digit of the integer. Make up for four.
①Example: Convert binary 11101001.1011 to hexadecimal

Get the result: Convert binary 11101001.1011 to hexadecimal as E9.B


② Example: Convert 101011.101 to hexadecimal

So get the result: Convert binary 101011.101 to hexadecimal The hexadecimal number is 2B.A



(2) Convert hexadecimal to binary
method : take one-quarter method, that is, decompose a hexadecimal number into four-digit binary number, and use the four-digit binary number to add according to the weight to make up For this hexadecimal number, the decimal point remains the same.
①Convert hexadecimal 6E.2 to binary number

So get the result: Convert hexadecimal 6E.2 to binary as 01101110.0010, that is, 110110.001

Four, octal and hexadecimal conversion
Method : Generally, it is not possible to directly convert each other, Generally, the octal (or hexadecimal) is converted to binary, and then the binary is converted to hexadecimal (or octal), and the decimal point position remains unchanged. Then for the corresponding conversion, please refer to the above binary and octal conversion and binary and hexadecimal conversion


5. Octal and decimal conversion
(1) Convert octal to decimal
Method : Add by weight, that is, the number on each bit of octal Multiply by the bit weight and add the sum to get the decimal number.
Example: ① Convert the octal number 67.35 to decimal

(2) Converting
decimal to octal There are two ways to convert decimal to octal:
1) Indirect method: first convert decimal to binary, and then convert binary to octal
2) Direct method: As we mentioned earlier, octal is derived from binary Therefore, we can use a method similar to the conversion from decimal to binary, or the conversion of the integer part and the conversion of the fractional part. Let's explain it in detail:
①Integer part
Method : divide by 8 and take the remainder, that is, each time the The integer part is divided by 8, the remainder is the number on the weight, and the quotient continues to be divided by 8, and the remainder is the number on the previous weight. This step continues until the quotient is 0. At the last reading, from From the last remainder to the first remainder.
②Decimal part
method : multiply by 8 to round up, that is, multiply the fractional part by 8, then take the integer part, continue to multiply the remaining fractional part by 8, then take the integer part, multiply the remaining fractional part by 8, and keep taking until the fractional part is zero. If it can never be zero, just like the rounding of decimal numbers, it is temporarily called 3 rounding 4 rounding.
Example: Convert the decimal number 796.703125 to an octal number
Solution : First divide the number into an integer part 796 and a fractional part 0.703125
Integer part

Fractional part

Therefore, get the result of converting decimal 796.703125 to octal to 1434.55
You can verify the above method, you can first Convert decimal and then convert to octal, so you can see if the result is the same.

Six , hexadecimal and decimal conversion
There are many similarities between hexadecimal and octal, you can refer to the above octal and decimal conversion and try it yourself Try converting between the two bases.
Through the conversion between various bases above, we can re-improve the previous conversion diagram:


This article introduces the conversion between binary, decimal, octal, and hexadecimal. Everyone should pay attention to the conversion method and steps when converting, especially the decimal conversion is between the three bases. To be divided into integer part and decimal part, the last is the position of the decimal point. However, to ensure that there are no mistakes in the test, you still need to practice regularly, so that practice makes perfect.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326780807&siteId=291194637