I. Contents of this Paper

  1. Contents of this article
  2. Contexts
  3. Base conversion algorithm (Convert)
    1. (two, eight, hexadecimal) → (decimal)
      1. Binary → decimal
      2. Octal to decimal
      3. Hexadecimal to decimal
    2. (decimal) → (two, eight, hexadecimal)
      1. Decimal → binary
      2. Decimal → octal
      3. Decimal to hexadecimal
    3. (binary) ↔ (8, hexadecimal)
      1. Binary to octal
      2. Octal to binary
      3. Binary to hexadecimal
      4. Hexadecimal to binary
    4. (octal) ↔ hexadecimal
      1. Octal to hexadecimal
      2. Hexadecimal to octal
  4. Further reading
  5. References

2. Context

Use SQL to convert decimal integers to hexadecimal. For the SQL code, see: SQL Server base conversion function, in fact, it is based on two, eight, ten, hexadecimal conversion formula, conversion between the base is very basic knowledge, but I found that there is no one on the network can put it said clear, simple, easy to understand the article, so I just write this article idea, Hopefully, you won’t have to worry about switching between bases anymore.

Here is the structure of the relationship between two, eight, ten, and hexadecimal:

(Figure1: base relation structure diagram)

This figure will be divided into four parts below, and each part will be explained in the form of pictures and texts:

  1. (two, eight, hexadecimal) → (decimal);
  2. (decimal) → (two, eight, hexadecimal);
  3. (binary) ↔ (eight, hex);
  4. (octal) at hexadecimal.

Iii. Base Conversion Algorithm

Different letters are added after numbers to indicate different carry systems. B (Binary) for Binary, O (Octal) for Octal, D (Decimal) or without Decimal, and H (Hexadecimal) for Hexadecimal. For example: (101011) B = (53) O = D (43) = H (2 B)

(a) (two, eight, hexadecimal) → (decimal)

(Figure2: other bases converted to decimal)

  • Binary → decimal

Method: The binary number is calculated from the lowest to the highest (that is, from right to left). The 0th bit has the weight of 2 to the 0, the first bit has the weight of 2 to the 1, and the second bit has the weight of 2 to the 2, and so on. The sum of the final result is the decimal value.

Example: The steps to convert binary (101011)B to decimal are as follows:

1. 1 x 2^0 = 1;

1 x 2^1 = 2;

X 2^2 = 0;

1 x 2^3 = 1 x 2^3 = 2

X 2^4 = 0 x 2^4 = 0

5. 1 x 2^5 = 32;

1+2+0+8+0+32=43, (101011)B=(43)D

Method: Octal numbers are computed from the lowest to the highest (right to left), with the 0th bit having the power of 8 to the 0, the first bit having the power of 8 to the 1, the second bit having the power of 8 to the 2, and so on, adding up the final result to the decimal value.

Octal is a decimal number that uses the octal numbers 0 to 7 to express a number.

Example: The steps to convert octal (53)O to decimal are as follows:

1. 0 x 8^0 = 3;

5 x 8^1 = 40

3. (53)O=(43)D.

  • Hexadecimal to decimal

Method: The hexadecimal number is calculated from the lowest to the highest (that is, from right to left). The 0th digit has the weight of 16 to the 0 power, the first digit has the weight of 16 to the 1 power, and the second digit has the weight of 16 to the 2 power, and so on. The sum of the final result is the decimal value.

The hexadecimal number is 0123456789ABCDEF.

Example: The steps to convert hexadecimal (2B)H to decimal are as follows:

1. (x) 2. (x) 3.

(1) 2 x 16^1 = 32;

(2B)H=(43)D (2B)H=(43)D

(2) (decimal) → (two, eight, hexadecimal)

(Figure3: decimal conversion to other bases)

  • Decimal → binary

Method: To divide the remainder of 2, that is, each time the integer part is divided by 2, the remainder is the number of the weight, and the quotient continues to divide by 2, the remainder is the number of the last weight, this step continues until the quotient is 0, the final reading, starting from the last remainder, until the first remainder.

Example: The steps to convert decimal (43)D to binary are as follows:

1. Divide quotient 43 by 2, and the remainder of quotient 21 is 1.

2. Divide quotient 21 by 2, the remainder of quotient 10 is 1.

Divide quotient 10 by 2, and the remainder of quotient 5 is 0.

4. Divide quotient 5 by 2, the remainder of quotient 2 is 1.

5. Divide quotient 2 by 2, the remainder of quotient 1 is 0.

6. Divide quotient 1 by 2, the remainder of quotient 0 is 1;

7. The reading, since the last digit is obtained by dividing it by 2 many times, is the highest digit. The reading number is read forward from the remainder of the last digit, 101011, i.e. (43)D=(101011)B.

(Figure4: illustrated decimal → binary)

  • Decimal → octal

Method 1: Divide the remainder of 8, that is, each time you divide the whole number by 8, the remainder is the number on the weight, and the quotient continues to divide by 8, the remainder is the number on the weight of the previous, this process continues until the quotient is 0, the final reading, starting from the last remainder, until the first remainder.

Example: The steps to convert decimal (796)D to octal are as follows:

1. Divide the quotient 796 by 8, and 99 has a remainder of 4;

2. Divide quotient 99 by 8, and the remainder of quotient 12 is 3.

Divide quotient 12 by 8, the remainder of 1 is 4.

4. Divide quotient 1 by 8, the remainder of quotient 0 is 1;

5. The reading, which is the highest since the last digit is obtained by dividing it by 8 many times, reads the number forward from the remainder, 1434, i.e. (796)D=(1434)O.

(Figure5: illustrated decimal → octal)

Method 2: Use the indirect method, first convert decimal to binary, then binary to octal;

(Figure6: illustrated decimal → octal)

  • Decimal to hexadecimal

Method 1: Mod by 16, that is, each time you divide the whole number by 16, the remainder is the weight of that bit, and the quotient continues to divide by 16, and the remainder is the weight of the last bit. This process continues until the quotient is 0, and at the last reading, starting from the last remainder, all the way to the first remainder.

Example: The steps to convert decimal (796)D to hexadecimal are as follows:

1. Divide the quotient 796 by 16. The remainder of 49 is 12, corresponding to hexadecimal C.

2. Divide quotient 49 by 16, and the remainder of 3 is 1.

3. Divide quotient 3 by 16, the remainder of quotient 0 is 3.

4. The reading, since the last digit is obtained by dividing it by 16 many times, is the highest digit. The reading number is read forward from the remainder of the last digit, 31C, i.e. (796)D=(31C)H.

(Figure7: illustrated decimal → hexadecimal)

Method 2: Use the indirect method, first convert decimal to binary, and then convert binary to hexadecimal;

(Figure8: illustrated decimal → hexadecimal)

Put it into hexadecimal at least once.

(Figure9: binary conversion to other base)

  • Binary to octal

Method: take three in one method, that is, from the binary decimal point as the demarcation point, left (right) every three bits into one, and then add the three binaries according to the weight, and then arrange in order, the position of the decimal point is the same, the number we get is octal number. If you take three places to the left (right), when you take the highest (lowest) digit, if you can’t make up three places, you can add 0 to the leftmost (rightmost) decimal point, that is, the highest (lowest) digit of the integer, so as to make up three places.

Example: Convert binary (11010111.0100111)B to octal as follows:

1. Before the decimal point 111 = 7;

2. 010 = 2;

3. 11 completion is 011,011 = 3;

4. Decimal point 010 = 2;

5. 3.

6. 1 completion = 100,100 = 4;

7. Reading from high to low, i.e. (11010111.0100111)B=(327.234)O

(Figure10: illustrated binary to octal)

Binary and octal encoding corresponding table:

binary

octal

000

0

001

1

010

2

011

3

100

4

101

5

110

6

111

7

 

  • Octal to binary

Method: divide an octal number into three binary numbers. Add the three binary numbers by weight to make up the octal number. The decimal position remains the same.

Example: Convert octal (327)O to binary as follows:

1. 3 = 1.

2. 2.

3. 3.

011010111 =(327)O=(11010111)B

(Figure11: illustrated octal to binary)

  • Binary to hexadecimal

Method: take four in one method, namely from the binary decimal point for the demarcation point, left (right) every four take one, and then add the four binary by weight, and then, in order to arrange, the position of the decimal point unchanged, the number is the hexadecimal number we seek. If you take four bits to the left (right), when you take the highest (lowest) bits, if you can’t make up the four bits, you can add 0 to the leftmost (rightmost) decimal point, that is, the highest (lowest) bits of the integer to make up the four bits.

Example: The steps to convert binary (11010111)B to hexadecimal are as follows:

1. 0111 = 7;

2.

(11010111)B=(D7)H.

(Figure12: illustrated binary to hexadecimal)

  • Hexadecimal to binary

Method: take a quarter method, will be a hexadecimal number into four binary number, with four binary by weight add up to the hexadecimal number, the decimal position remains the same.

Example: The steps to convert hexadecimal (D7)H to binary are as follows:

1. D = 1101;

2. 7.

(D7)H=(11010111)B.

(Figure13: illustration hexadecimal to binary)

Do you know how to put it in hexadecimal?

(Figure14: octal to hexadecimal conversion)

  • Octal to hexadecimal

Method: Convert octal to binary and then binary to hexadecimal with the same decimal position.

Example: The steps to convert octal (327)O to hexadecimal are as follows:

1. 3 = 1.

2. 2.

3. 3.

4. 0111 = 7;

5. 1.

6. Reading, reading from high to low, D7, i.e. (327)O=(D7)H.

(Figure15: illustration octal to hexadecimal)

  • Hexadecimal to octal

Method: Convert hexadecimal to binary, and then convert binary to octal, with the decimal point position unchanged.

Example: The steps to convert hexadecimal (D7)H to octal are as follows:

1. 7.

2. D = 1101;

3. 0111 = 7;

4. 2.

5. 3.

6. Reading, reading from high to low, 327, i.e. (D7)H=(327)O.

(Figure16: illustrated hexadecimal to octal)

Four. Read more

1. Decimal conversion:

(ABC.8C)H=10×16^2+11×16^1+12×16^0+8×16^-1+12×16^-2

= 2560 + 176 + 12 + 0.5 + 0.046875

D = (2748.546875)

2. Calculation of negative power:

^ ^ 2-5 = 2 (0 to 5) = 2 ^.two survivors 1/2 ^ ^ 5 = 5

The same base divided by the same power, the same base, minus the exponent, the other way around

3. We need to understand the mathematical relationship that 23= 8,24 =16, from which octal and hexadecimal are derived, i.e., three bits for one octal and four bits for one hexadecimal. Next, remember the four numbers 8, 4, 2, 1 (23=8, 22=4, 21=2, 20=1).

V. References

Binary, octal, decimal, hexadecimal conversion

How to convert binary to octal

Author: listen to the wind blowing rain Source: http://www.cnblogs.com/gaizai/ email: [email protected] copyright: in this paper, the copyright belongs to authors and bloggers gardens of reproduced: welcome to reprint, you must retain the original link motto: don’t like because not && because will like it