Number system conversion is a method of counting using symbols. Number system conversion consists of a set of numerical symbols and two basic factors: "base" and "place value".
The radix refers to the number of digits (symbols used to represent "quantities" in a number system) used in a positional numeral system.
Place value refers to the unit value corresponding to each fixed position in a positional system.
Number systems, also known as base systems, are familiar to anyone who has used a computer. Commonly used number systems include binary, octal, decimal, and hexadecimal. The difference between them lies in how they are used in arithmetic operations: carrying over at each digit. For example, binary carries over at 2, and decimal (0-9) carries over at 10. I won't go into the specific usage today; I'll mainly discuss the conversions between them (we'll only cover integers today).
I. Conversion between binary and decimal
1. Decimal to Binary Conversion
The method is as follows: the decimal number division by 2 and taking the remainder method, that is, the decimal number is divided by 2, the remainder is the number of the place value, the quotient is divided by 2 again, and this step is continued to be calculated until the quotient is 0.
(See the image below for specific usage instructions)
2. Binary to Decimal Conversion
The method is as follows: expand the binary number according to its weights and add them together to get the decimal number.
(See the image below for specific usage instructions)
II. Conversion between binary and octal
1. Binary to Octal Conversion
The method is as follows: Expand the 3 binary digits according to their weights and add them together to get a 1-digit octal number. (Note that the conversion from 3 binary digits to octal starts from right to left, and 0s are added if necessary).
(See the image below for specific usage instructions)
2. Octal to Binary Conversion
The method is as follows: the octal number is divided by 2 and the remainder is taken to obtain the binary number. Each octal number is 3 binary numbers. If there are not enough, zeros are added to the leftmost side.
III. Conversion between binary and hexadecimal
1. Binary to hexadecimal conversion
The method is similar to the binary-to-octal conversion method, but for octal it combines three digits into one, and for hexadecimal it combines four digits into one. (Note that when converting 4-bit binary to hexadecimal, the conversion starts from right to left, and zeros are added if necessary).
2. Hexadecimal to binary conversion
The method is as follows: Divide the hexadecimal number into a binary number by dividing it by 2 and taking the remainder. Each hexadecimal number is represented by 4 binary digits. If the binary number is insufficient, add zeros to the leftmost side.
IV. Conversion between Decimal, Octal, and Hexadecimal Numbers
1. There are two methods to convert decimal to octal or hexadecimal.
First: Indirect method—convert decimal to binary, and then from binary to octal or hexadecimal. Illustrated explanations of usage are omitted here.
Second: Direct method—convert decimal to octal or hexadecimal by dividing by 8 or 16 and taking the remainder until the quotient is 0.
2. Convert octal or hexadecimal to decimal.
The method is as follows: expand the octal and hexadecimal numbers according to their weights and add them together to get the decimal number.
V. Conversion between hexadecimal and octal
There are two methods for converting between octal and hexadecimal.
The first method: They can be converted to binary first, and then converted to each other.
The second method: the conversion between them can be done by first converting them to decimal and then converting them to each other.
The usage of the images will not be explained here.
VI. Negative Numbers
The number system conversion for negative numbers is slightly different.
First, write the negative number in its two's complement form (not discussed here), and then proceed according to the method of converting binary to other bases.
Example: To convert -9 to octal form, we have:
The two's complement of -9 is 1111111111110111. Start by dividing the last three digits into three parts, adding 0 if the result is less than three parts.
111---->7
110---->6
111---->7
111---->7
111---->7
001---->1
Then we write the result in order from bottom to top: 177767. So 177767 is the octal form of the decimal number -9.
In fact, it's the same whether it's converted to any base.
The most common mistakes beginners make!
Error: (-617)D = (-1151)O = (-269)H
Reason analysis: If it is a positive number, the above approach is correct. However, due to the differences in the conversion between the original code, inverse code, and complement code of positive and negative numbers, it is incorrect to use the same approach for solving positive numbers to solve negative numbers.
The correct method is to first represent -617 in two's complement, and then convert it to octal and hexadecimal (two's complement).
Note: 16 bits are required for binary two's complement.
Correct answer: (-617)D = (176627)O = (fd97)H
Methods for converting negative decimal numbers to octal or hexadecimal
For example, (-12)10 = ( )8 = ( )16
Step 1: Convert to binary
1000000000001100
Step 2: Two's complement, invert and add one.
Note: The sign bit remains unchanged when inverting!
1111111111110100
Step 3: Convert to octal, which is a combination of three digits: 177764 (8)
Converted to hexadecimal, it is a combination of four digits: fff4(16)
7. Decimals
Recently, some friends have asked this question: "What is 0.8 in hexadecimal?"
0.8, 0.6, 0.2... There are indeed some problems in the conversion between number systems.
Take "0.8 in hexadecimal" for example!
No matter how many times you multiply by 16, the remainder is never exact; it always leaves a remainder of 0.8.
The specific method is as follows:
0.8 * 16 = 12.8
0.8 * 16 = 12.8
Take the integer part of each result as 12, which is C in hexadecimal.
If the question requires accuracy to three decimal places, then the result is 0.CCC.
If the question requires accuracy to four decimal places, then the result is 0.CCCC
It's OK now.
Disclaimer: This article is a reprint. If it involves copyright issues, please contact us promptly for deletion (QQ: 2737591964). We apologize for any inconvenience.