Home > Article > Backend Development > PHP basic tutorial
Conversion between binary, octal, decimal and hexadecimal 1. Conversion between decimal and binary
(1) Decimal to binary conversion, divided into integer part and decimal part
① Integer part
Method: divide by 2 Take the remainder and arrange it in reverse order, that is, divide the integer part by 2 each time, the remainder is the number on the position weight, and the quotient continues to be divided by 2, and the remainder is the number on the previous position weight. This step continues until Until the quotient is 0, when reading the last number, read from the last remainder to the first remainder. Here is an example:
Example: Convert decimal 168 to binary
The result is Convert decimal 168 to binary, (10101000)
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 the quotient 5 by 2, and the remainder of the quotient 2 is 1.
Step 7: Divide quotient 2 by 2, and the remainder of quotient 1 is 0.
Step 8: Divide the quotient 1 by 2, and the remainder of the quotient 0 is 1.
Step 9, reading, because the last digit is obtained after dividing by 2 many times, so it is the highest digit. Read the number forward from the last remainder, that is, 10101000
(2) Decimal part
Method: Multiply by 2 and round to an integer, arrange in order, that is, multiply the decimal part by 2, then take the integer part, continue to multiply the remaining decimal part by 2, then take the integer part, and multiply the remaining decimal part by 2, until Until the decimal part
is zero. If it can never be zero, it is the same as the rounding of decimal numbers. When retaining as many decimal places as required, the number will be rounded based on whether the next digit is 0 or 1. If it is zero, round it off. If it is 1, add one digit. In other words, 0 is rounded to 1. The reading should be read from the previous integer to the following integer. Example 1: Convert 0.125 to binary Multiply by 2 to get 0.25, then the integer part is 0 and the decimal part is 0.25;
The second step is to multiply the decimal part 0.25 by 2 and get 0.5, then the integer part is 0 and the decimal part is 0.5;
Third Step 1: Multiply the decimal part 0.5 by 2 to get 1.0, then the integer part is 1 and the decimal 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 (retained 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, We get 0.8, multiply 0.8 by 2, and keep multiplying until 1.6. In the end, it is impossible to get the decimal part to be zero. Therefore, at this time, we have to learn the decimal method to round, but binary only has 0 and 1, so The occurrence of 0 is rounded to 1. This is also caused by computer errors during conversion, but since there are many reserved digits and the accuracy is very high, it can be ignored.
Then, we can conclude that converting 0.45 to binary is approximately equal to 0.0111
The method introduced above is a 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 integers and decimal parts are converted separately
2) When converting integers, the division by 2 method is used to round up the remainder, while when converting decimals, the multiplication by 2 rounding method is used
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 is approximately equal to 10101000.0111.
(3) Convert binary to decimal without dividing into integers and decimal parts
Method: Add by weight, that is, multiply the number in each bit of the binary by the weight, and then add the sum to the decimal number. Example
Convert the binary number 101.101 to a decimal number.
The result is: (101.101)2=(5.625)10
What everyone needs to pay attention to when converting binary to decimal is
1) You must know the weight of each binary digit
2) You must be able to calculate each bit The value of
2. Conversion between binary and octal
First, 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 binary digits are used to represent an octal number, and four binary digits are used to represent a hexadecimal number.
Next, remember the four 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, starting from the decimal point of the binary system as the dividing point, take every three digits to the left (right) into one digit, and then add these three binary digits according to weight, and the resulting number is a one-digit, eight-digit binary system. The numbers are then arranged in order, the position of the decimal point remains unchanged, and the number obtained is the octal number we are looking for. If you take three digits to the left (right) and get to the highest (lowest) digit, if you cannot make up the three digits, you can add 0 to the leftmost (rightmost) of the decimal point, that is, the highest (lowest) digit of the integer. Make up three people. Example
①Convert the binary number 101110.101 to octal
Get the result: Convert 101110.101 to octal to 56.5
② Convert the binary number 1101.1 to octal
Get the result: Convert 1101.1 to The octal system is 15.4
(2) Convert octal to binary
Method: Take the one-third method, that is, decompose an octal number into three binary digits, and use the three-digit binary digits to add the octal number according to the weight. The decimal point position remains the same. Example:
① Convert the octal number 67.54 to binary
Therefore, convert the octal number 67.54 to the binary number 110111.101100, which is 110111.1011
As you can see from the above question, calculate the conversion of octal to binary
First of all, Expand each octal number from left to right to three digits, leaving the decimal point unchanged
Then, expand each digit into three digits: 22, 21, 20 (i.e. 4, 2, 1) to make up the number, that is, a× 22+ b×21 +c×20=the number in the bit (a=1 or a=0, b=1 or b=0, c=1 or c=0), arrange abc to get the binary number of the bit
Then, upconvert each bit into a binary number and arrange them in order
Finally, you get the number converted from octal to binary.
The above method is the exchange of binary and octal. What you need to pay attention to when doing the questions is
1) The exchange between them is the conversion between one digit and three digits, which is different from the conversion between binary and decimal.
2) When adding 0 and removing 0, please note that you must add 0 or remove 0 at the far left of the decimal point or the far right 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 to hexadecimal conversion
Method: Similar to binary to octal conversion, it is just a conversion of one digit (hexadecimal) and four digits (binary). The following will explain in detail
(1) Binary Convert to hexadecimal
Method: Take the four-in-one method, that is, use the binary decimal point as the dividing point, take every four digits to the left (right) into one, and then add these four binary digits according to weight to get The number is a sixteen-digit binary number, and then, arranged in order, the position of the decimal point remains unchanged, and the number obtained is the hexadecimal number we are looking for. If you take four digits to the left (right) and get to the highest (lowest) digit, if you cannot make up the four digits, you can add 0 to the leftmost (rightmost) of the decimal point, that is, the highest (lowest) digit of the integer. Make up four people.
①Example: Convert binary 11101001.1011 to hexadecimal
Get the result: Convert binary 11101001.1011 to hexadecimal to E9.B
②Example: Convert 101011.101 to hexadecimal
Therefore get results : Convert binary 101011.101 to hexadecimal into 2B.A
(2) Convert hexadecimal to binary
Method: Take the one-quarter method, that is, decompose a hexadecimal number into four binary digits Number, use four binary digits to add up the hexadecimal number, and the decimal point position remains the same.
① Convert hexadecimal 6E.2 to binary number
Therefore, the result is: Convert hexadecimal 6E.2 to binary to 01101110.0010, which is 110110.001
4. Conversion of octal and hexadecimal
Method: Generally, they cannot be converted directly to each other. Generally, octal (or hexadecimal) is converted to binary, and then binary is converted to hexadecimal (or octal). The decimal point position remains unchanged. Then for the corresponding conversion, please refer to the above conversion of binary to octal and conversion of binary to hexadecimal
5. Conversion of octal to decimal
(1) Conversion of octal to decimal
Method: Press the weighted addition method, that is, The decimal number is obtained by multiplying each bit of the octal number by the bit weight and then adding the sum.
Example: ①Convert the octal number 67.35 to decimal
(2) Convert decimal to octal
There are two methods to convert decimal to octal:
1) Indirect method: first convert decimal to binary, and then convert binary again Convert to octal
2) Direct method: As we said before, octal is derived from binary, so we can use a similar method to convert decimal to binary, or convert the integer part and the decimal part, as follows Let’s explain it in detail:
①Integer part
Method: Divide by 8 and take the remainder method, that is, divide the integer part by 8 each time, the remainder is the number on the place weight, and the quotient continues to be divided by 8, the remainder is the number on the previous place weight, and this step continues , until the quotient is 0. When reading the last number, start from the last remainder to the first remainder.
② Decimal part
Method: Multiply by 8 to round up the whole method, that is, multiply the decimal part by 8, then take the integer part, continue to multiply the remaining decimal part by 8, then take the integer part, and multiply the remaining decimal part by 8. Take until the decimal part is zero. If it can never be zero, it is just like the rounding of decimal numbers, temporarily named 3 rounding.
Example: Convert the decimal number 796.703125 to octal number
Solution: First divide this number into the integer part 796 and the decimal part 0.703125
The integer part
The decimal part
Therefore, we get the result decimal 796.70 Convert 3125 to octal to 1434.55
You can verify the above method. You can first convert decimal and then convert to octal to see if the results are the same
6. Conversion between hexadecimal and decimal
There are differences between hexadecimal and octal There are many similarities. You can refer to the above conversion between octal and decimal to try the conversion between these two systems.