Java one thousand Q: among the various number system and number system conversion algorithm Detailed

We use numbers in life, all the way to the decimal count. A little common sense knows that a computer, the computer counting in binary manner, and the other represents a color often used hexadecimal numbers in the computer, in addition octal numbers will be used in some other areas. So binary, octal, decimal, hexadecimal numbers, what is the count? Between each other in terms of how they are? This article will explain in detail to these two issues.

A variety of binary counting method

First of all we are most familiar decimal. 10 decimal notation raw figures 0-9 respectively, when we want to represent a further large numbers 1 to 9, a number of bits is not enough, and must be represented by two digits, which is 10, then 1 than the number of 10 ... 11 and so this can be expressed arbitrarily large number.
The binary notation used in computers, binary counting only two original numbers is 0 and 1, to represent a digital 1 larger than 1 on the need to represented by two digits, which is 10, then 11, then after ... 100 in binary notation is the largest number is 1, then we should add the carry. Such so can represent any number.
Through the study of binary notation, surely we already know the characteristics of octal numbers, that is a total of eight original numbers are 0-7, when large numbers also represent a ratio of 7 1 must carry a.
So come to talk about hexadecimal notation. Hexadecimal notation, there are 16 original digital, original digital before 10 we will be represented by 0-9, but we are accustomed to using Arabic numerals only 10 digits 0-9, and now six short of original digital, how to do it? We will use to replace the letters, so the original number of 11 to A, the second B is the number of the original 12, and so, the original number is 16 F. The F converted to decimal number is actually 15. When the digital representation F bigger than 1, we need to carry, so F is the number again after 10, 11 ... little friends can be very technical methods are not used to hex, because suddenly ran out of some of the letters It represents a number, in fact, use more natural get used to it.

Second, conversion of the number system

Below us, saying how conversion between these number system. 4 kinds of hexadecimal numbers just mentioned, conversion, then there are 12 kinds of situations, see below:
Java one thousand Q: among the various number system and number system conversion algorithm Detailed
From this picture we can see that all cases of mutual conversions between various number system: four kinds of number mutual connection between the system, a total of 12 lines can be connected. The first reaction of many small partners is this: so many cases, but also remember a lot of transformation rules. In fact, no need to worry, we can put 12 cases were divided into three types, and each type of control algorithm on it. 12 kinds of the case can be divided into the following three categories

  • Other hexadecimal number is converted to decimal
  • Decimal number converted to hexadecimal number of other
  • Conversion between other number system
    first for us to say what other hexadecimal number is converted to decimal number. When we get a decimal number, such as 168, then 168 168 Why is it? Little friends will say this is not nonsense it? 168 is still not a 168 number? In fact, this figure is 168 by the three original numbers 1, 6 and 8 consisting of the three original digital occupy one hundred, ten and a bit so that we can put this number 168 broken down into the following form:

Java one thousand Q: among the various number system and number system conversion algorithm Detailed
Calculating the value of the above equation is obtained 168, the 3, 6 and 8 which can be represented by the original digital 168 line up.
It should be emphasized detail, because we are talking about 10 decimal digits, the three original numbers are multiplied by the base 10 of the n-th power. According to this principle, if the hexadecimal number in question is 2, then calculates a raw digital number should be multiplied by the n-th power of 2 as a substrate, and then each of the digital sum, and finally reached this number can be specific a value, such as a binary number 101, in accordance with the algorithm just, 101 can be broken down into the following form:
Java one thousand Q: among the various number system and number system conversion algorithm Detailed
the value of the above calculation equation 5 is obtained. This is the basic principle of binary conversion to decimal numbers.
Similarly, we can also convert octal and hexadecimal to decimal, hexadecimal numbers such 1AF, can be decomposed into the following form:
Java one thousand Q: among the various number system and number system conversion algorithm Detailed
We when calculating the direct A and F are replaced by their corresponding decimal number 10 and 15, the above calculation equation resulting decimal number is 431. A few examples, I believe we already have a number of other binary to decimal conversion algorithm. The algorithm routine and M is the number of bits on each end of the n-1 th power multiplied and the multiplication results are added up, it is noted that: several decimal binary conversion, it is a few M, For example, take the binary to decimal, this is M 2. And n is called from the right the first of several, for example from the right bit digital number 3, n is 3, and n-1 is 2.

Just now we are talking about is the first type, the other is the hexadecimal number is converted to decimal, hexadecimal other number below how to convert a decimal number we come to talk about. How to convert a decimal number to hexadecimal number of other algorithms we can call it "removed remainder Act." The "remainder removed Method" refers to the constant divided by N with a decimal number, until the quotient is zero, then the remainder of the division operation each resulting string together to complete the conversion. To note here is that if you want to convert into a binary number, it is the constant divided by 2, the same token, if you want to convert to an octal number, it is constantly divided by 8. We have to convert decimal to binary as an example to explain this algorithm for everyone. For example, we want to be converted to decimal number 13 in binary, you can complete the conversion in the following ways:

Java one thousand Q: among the various number system and number system conversion algorithm Detailed
Calculation process as shown above: the 13 divided by 2, if the quotient is not zero, then continued to divide the quotient obtained by dividing 2, up until the quotient is 0, and finally the operation of each of the resulting division remainder from bottom to top to string together, the result is 1101.
Here again an example in terms of decimal to hexadecimal numbers. For example, the 1835 figure is converted to hexadecimal, the algorithm is the same, but this is by constantly divide that number by 16, the process as shown below:
Java one thousand Q: among the various number system and number system conversion algorithm Detailed
replace the remainder of greater than 9 for the remainder of the letters, that is, Alternatively the 11 is B, thus obtained is converted to 1835 to hexadecimal number 72B

Above we have explained the two types of conversion between the number system, they are other binary to decimal and decimal to hexadecimal other. Now only a third, that is, switching between other hexadecimal numbers. First we talk about binary to hexadecimal number of algorithms. Speaking before the algorithms must speak about binary numbers and hexadecimal numbers have anything to do. We know that only two of the original binary numbers are 0 and 1, that is, only two cases every bit binary number, either 0 or 1. According to permutations and combinations of the algorithm, if we want to rank the four cases, only a binary number can not be achieved because each bit has only two cases of binary numbers. To arrange the four cases, it must be 2 digits, empathy, want to rank the eight kinds of circumstances, to use the 3-digit, want to arrange the 16 kinds of circumstances, it must be 4 digits. The hexadecimal number with 16 original digital, hexadecimal number that is just a bit to have the numbers on the possibility of 16 kinds of change. So we can easily understand a truth, that is: one digit can represent hexadecimal number, the equivalent of four-digit binary number can be represented.
According to this idea, when we convert a binary number to a hexadecimal number, the first of every 4-bit binary number into a set, each set of binary digits were converted to a hexadecimal number. When divided from the right beginning, each divided into four groups 1, if the plan is not enough to the left to find four, let alone the rest of these is divided into a number of groups. For example, the binary number 101,101,110,001,110 converted to hexadecimal as follows:
Java one thousand Q: among the various number system and number system conversion algorithm Detailed
As shown above: starting from the right, each divided into a group of four, less than four separate divided into a left group, each group after the first binary number into a decimal number, then the number is greater than 9 in place of the letter, and finally produces a result 5B8E conversion.
This example is to convert a binary number to hexadecimal, when the conversion of the 4-bit binary number is divided into a set conversion, if you want to convert a binary number to octal how to do it? Many students will certainly immediately think, at the time of conversion of the 3-bit binary number is divided into a group, why is divided into a group of three it? The reason is simple, because each 3-bit binary number can rank the eight kinds of circumstances, and octal numbers for each one there are eight kinds of possibilities for change.
We still are exemplified with just this binary digits, the conversion process is as follows:

  1. Java one thousand Q: among the various number system and number system conversion algorithm Detailed
    Converts the result to 55616. Because the 3-bit binary number can not be a combination of a larger number than 8, so the situation will not replace numbers with letters appear, then we get this figure itself is already an octal number up.
    The above explanation is converted to hexadecimal and octal binary number, then how octal binary numbers into how to convert a hexadecimal number to octal? Octal how converted to hexadecimal and so on. These types of situations is definitely not easy to direct conversion, in fact, we do not have to worry, we can be accomplished in two steps, first of all some kind of hex numbers to decimal numbers, decimal number for the media, and then converted to another number system on it. Take for example just numbers, if we want to octal number 55616 to hexadecimal numbers, you can first convert the octal number for the decimal number 23438, then the decimal number 23438 to hexadecimal number 5B8E on it. In short, in all cases not convenient conversion, can be converted to decimal, and then the second conversion. Thus, between any two numbers can be made to achieve the conversion.

If you want to learn Java programming system, and welcome to my video lessons on this site.

Guess you like

Origin blog.51cto.com/2266836/2468308