How to convert between hex

How to convert between hex

Text Keywords: binary, decimal, binary conversion, octal, hexadecimal

First, hex

Digital and computational methods we came into contact with were based on decimal, hexadecimal then the meaning of which is a kind counting method. According to rules corresponding decimal carry, the same string of digits will correspond to different sizes in different band, so the program would have a clear binary digital identification.

1. Decimal

10 is filled into a decimal, then the range of numbers on each one will only be 0-9, is also the default radix to be used.

2. Binary

2 into the binary 1 is full, then the range of numbers on each one will only be 0 or 1, Java is used at the beginning 0b.

3. octal

Octal is full 8 into 1, then the range of numbers on each one will only is 0 ~ 7, Java is used at the beginning 0.

4. Hex

Hexadecimal 16 is filled into one, for a number greater than 10 is representative of starts with the letter A, i.e. A represents 10, B representatives 11, F 15 on behalf of, the range of numbers on each one will only 0 ~ F, Java is used at the beginning of 0x.

Second, the decimal and binary conversion

In the very beginning to programming and always learn about binary conversion, because it is binary computer use at work, a lot of bit manipulation operation is also carried out in binary, so we have to grasp the related binary decimal conversion.

1. binary to decimal

The other will convert a hexadecimal number to decimal process is actually carried out at the corresponding hexadecimal. Before we convert our first look at the most familiar decimal, such as: 1367. We see this number will not hesitate to say: one thousand three hundred sixty-seven, this is something we take for granted, but what specific procedure is it?

  • 1367 = 7 × 1 + 6 × 10 + 3 × 100 + 1 × 1000
  • 1367 = 7 × 10^0 + 6 × 10^1 + 3 × 10^2 + 1 × 10^3

As can be seen from the above step, in fact, a number of interpretation is actually from right to left, and just because we have too much understanding for decimal so ignore this step, then use this figure to feel: 1237173927, I guess you must It is from right to left, a few bits from the start, in the end is how much, right?
Then the other binary arithmetic rules, too, and now we have to read several binary number.

  • 101:1 × 2^0 + 0 × 2^1 + 1 × 2^2 = 5
  • 10010:0 × 2^0 + 1 × 2^1 + 0 × 2^2 + 0 × 2^3 + 1 × 2^4 = 18
  • 1010101:1 × 2^0 + 0 × 2^1 + 1 × 2^2 + 0 × 2^3 + 1 × 2^4 + 0 × 2^5 + 1 × 2^6 = 85

Congratulations, our base for the conversion has been completed. The method is from right to left, the multiplied result is added. At the same time, we note that, as long as the last bit is 0, then the number must be divisible by 2, other band also has this rule (like a certain number of zero bits can be divisible by 10).

2. decimal to binary

  • normal method:

Converts a decimal number to a binary process is actually a constantly count division and the process of recording the remainder, since it is converted to binary, then our divisor is 2, the specific process is as follows:
decimal number: 37 -> binary result : 100,101
How to convert between hex

  • Fast small digital conversion method:

For not a big number actually there is a faster method of conversion, but only if we want the result of the operation of the power of 2 more familiar, basically memory 2 to the power of 10 is 1024 will be enough. Our approach is a decimal number in the form of direct dismantling several n-th power of 2 plus and, starting from the largest number.
37 For example, the maximum number is binary 32, followed by 4, then 1, then the equation is: 37 + 4 + 1 = 32.

  • 5 is a power of 2 32
  • 4 is a square of 2
  • 1 is a zero power of 2

Therefore, position 6 of the binary numbers corresponding to the third position, the first number is 1 at the position (an offset needed), and 0 otherwise. In this way it is possible to quickly know the most significant bit is position 1, can be very fluent in accordance with the written order from left to right straight binary, but for large numbers is not as useful.

Guess you like

Origin blog.51cto.com/10984944/2452863