In programming, we usually use 10 hexadecimal notation ...... C/C ++ is a high-level language.

For example:

Int A = 100, B = 99;

However, because the representation of data in a computer exists in binary format, sometimes binary can be used to solve the problem more intuitively.

However, the binary number is too long. For example, the int type occupies 4 bytes and 32 bits. For example, if the value is 100, the value expressed in the binary number of the int type is:

0000 0000 0000 0000 0110 0100

No one would like to think about or operate on such a long number. Therefore, C and C ++ do not provide a method to directly write binary numbers in code.

This problem can be solved in hexadecimal or octal. Because,**The larger the number, the shorter the expression length.**. However, why is it 16 or 8 hexadecimal instead of 9 or 20?

2, 8, and 16 are respectively the power 1 of 2, the Power 3, and the Power 4. This allows the three hexadecimal systems to directly convert each other. In octal or hexadecimal mode, the binary number is shortened, but the expression of binary number is maintained. In the following course about hexadecimal conversion, you can find this point.

6.2 convert binary number to decimal number 6.2.1 convert to decimal number

0th bits in binary are 0 to the power of 2, and 1st bits are 1 to the power of 2 ......

Therefore, there is a binary number: 0110 0100, which is converted to a 10-digit system:

The following is a vertical layout:

0110 0100 to decimal

0th-Bit 0*20 = 0

1st-Bit 0*21 = 0

2nd-bit 1*22 = 4

3rd-Bit 0*23 = 0

4th-Bit 0*24 = 0

5th-bit 1*25 = 32

6th-bit 1*26 = 64

7th-Bit 0*27 = 0 +

---------------------------

100

The horizontal calculation is as follows:

0*20 + 0*21 + 1*22 + 1*23 + 0*24 + 1*25 + 1*26 + 0*27 = 100

The number multiplied by 0 is 0, so we can skip the bit with a value of 0:

1*22 + 1*23 + 1*25 + 1*26 = 100

6.2.2 convert octal to decimal

In October, it means every 8 to 1.

The eight-digit number ranges from 0 ~ 7.

The 0th bits in eight dimensions are 0 to the power of 8, the 1st bits are 1 to the power of 8, and the 2nd bits are 2 to the power of 8 ......

Therefore, there is an octal digit: 1507, which is converted to decimal:

Vertical representation:

1507 to decimal.

0th-bit 7*80 = 7

1st-Bit 0*81 = 0

2nd-bit 5*82 = 320

3rd-bit 1*83 = 512 +

--------------------------

839

Similarly, we can use horizontal calculation:

7*80 + 0*81 + 5*82 + 1*83 = 839

The result is that the number of octal digits is 1507 to 839 in decimal format.

6.2.3 expression of the octal number

In C and C ++, how does one express an octal number? If the number is 876, we can conclude that it is not an octal number, because the octal number cannot contain more than 7 Arabic numbers. However, if the number is 123, 567, or 12345670, it is possible to set the number to octal or decimal.

Therefore, C and C ++ rules,**If you want to specify that a number uses an octal value, you must add a value of 0 before it.**For example, 123 is in decimal format, but 0123 indicates octal. This is the expression of Octal numbers in C and C ++.

Since neither c nor C ++ provides the expression of binary numbers, we learned octal here. The second method of expressing the value of CTC ++ is used.

Now, for the same number, for example, 100, we can use the normal 10 hexadecimal expression in the Code, for example, during variable initialization:

Int A = 100;

We can also write:

Int A = 0144; // 0144 is**Octal**100; how to convert a 10-digit number to an 8-digit number will be learned later.

Remember, you cannot lose the first zero when expressing it in octal. Otherwise, the computer is regarded as a 10-digit system. However, when the number of octal characters is used, the value 0 cannot be used, which is the "Escape Character" expression we learned before for expressing characters.

6.2.4 use of Octal numbers in escape characters

We have learned how to use an escape character '\' and a special letter to represent a character. For example, '\ n' indicates line ), '\ t' indicates the Tab character, and' \ 'indicates the single quotation mark. Today, we have learned another method to use escape characters: Escape Character '\' followed by an octal number, used to indicate characters whose ASCII code is equal to this value.

For example, check the ASCII code table in Chapter 5th and find the question mark character (?) The ASCII value of is 63, so we can convert it into an octal value: 77, and then use '\ 77' To Represent '? '. Because it is octal, it should be written as '\ 077', but because C, C ++ does not allow the use of a slash plus a 10-digit number to represent characters, so here 0 can be left empty.

In fact, we seldom use escape characters and Octal numbers to represent a character in actual programming. Therefore, we only need to understand the content in section 6.2.4.

6.2.5 convert the hexadecimal number to the decimal number

In decimal format, two Arabic numbers are used: 0 and 1;

8 digits: 0, 1, 2, 3, 4, 5, 6, and 7;

10 in hexadecimal notation, with 10 Arabic numerals: 0 to 9;

In hexadecimal notation, sixteen Arabic numerals ...... Wait, the Arab, or the Indian, only invented 10 numbers?

In hexadecimal notation, the number is 16 to 1, but we only have 0 ~ 9 These 10 numbers, so we**Use the letters A, B, C, D, E, and F to represent 10, 11, 12, 13, and 15 respectively.**. Uppercase letters are not case sensitive.

The 0th bits in hexadecimal notation are 0 to the power of 16, 1st bits are 1 to the power of 16, and 2nd bits are 2 to the power of 16 ......

Therefore, in the Nth (N starts from 0) bit, if it is a number x (x is greater than or equal to 0, and X is less than or equal to 15, that is, F) it indicates the N power of x * 16.

Suppose there is a hexadecimal number 2af5, how can we convert it into a hexadecimal number?

Use vertical calculation:

2af5 to 10:

0th bits: 5*160 = 5

1st bits: F * 161 = 240

2nd bits: A * 162 = 2560

3rd bits: 2*163 = 8192 +

-------------------------------------

10997

Direct calculation is:

5*160 + F * 161 + A * 162 + 2*163 = 10997

(Remember, in the above calculation, a represents 10, and f Represents 15)

Now we can see that the key to converting all hexadecimal values into a decimal value is that their respective weights are different.

Suppose someone asks you, why is the ten-in-one number 1234 one thousand two hundred and thirty-four? You can give him the following formula:

1234 = 1*103 + 2*102 + 3*101 + 4*100

6.2.6 expression of hexadecimal numbers

If no special writing format is used, the hexadecimal number will be mixed with the hexadecimal number. A random number: 9876, it cannot be seen that it is in hexadecimal or 10 hexadecimal format.

C, C ++,**Hexadecimal number must start with 0x**. For example, 0x1 indicates a hexadecimal number. 1 indicates a decimal number. For example, 0xff, 0xff, 0x102a, and so on. X is not case sensitive. (Note: 0 in 0x is the number 0, not the letter O)

Some examples are as follows:

Int A = 0x100f;

Int B = 0x70 +;

So far, we have learned all the hexadecimal expressions: 10 hexadecimal, 8 hexadecimal, and 16 hexadecimal numbers. The last point is very important. In C/C ++, there are positive and negative numbers in hexadecimal notation. For example, 12 indicates positive 12, while-12 indicates negative 12.**The octal and hexadecimal values can only be unsigned positive integers.**If you are in the Code:-078, or write:-0xf2, C, C ++, it is not treated as a negative number.

6.2.7 use of hexadecimal numbers in escape characters

An escape character can also be expressed as a hexadecimal number. As mentioned in section 6.2.4 '? 'Character, which can be expressed as follows:

'? '// Enter the character Directly

'\ 77' // use the octal sequence. The value 0 at the beginning can be omitted.

'\ 0x3f' // hexadecimal format

Similarly, this section is only used for understanding. Except for the null characters, the octal digit '\ 0' is used. We rarely use the last two methods to represent a single character.

6.3 decimal number to binary, octal, and hexadecimal number 6.3.1 10 hexadecimal number to binary number

Give you a decimal number, for example, 6. What if I convert it into a binary number?

The 10th hexadecimal number is converted to the binary number, which is a process of consecutive division of 2:

**Divide the number to be converted by 2 to obtain the quotient and remainder,**

**Divide the quotient by 2 until the quotient is 0. Finally, sort all the remainder in reverse order. The result is the conversion result.**

Sounds confused? We will illustrate it with examples. For example, convert 6 to the binary number.

"Divide the number to be converted by 2 to get the quotient and remainder ".

So:

The number to be converted is 6, 6 then 2.**The quotient is 3, and the remainder is 0.**. (Don't tell me you won't calculate 6 then 3 !)

"Divide the merchant by 2 until the merchant is 0 ......"

The current business is 3, not 0, so continue to divide by 2.

So: 3 then 2, get**The quotient is 1, and the remainder is 1.**.

"Divide the merchant by 2 until the merchant is 0 ......"

The current business is 1, not 0, so continue to divide by 2.

Then: 1 second 2, get**The quotient is 0, and the remainder is 1.**(Take a pen and paper to calculate it. 1, 2, is it 0, more than 1 !)

"Divide the merchant by 2 until the merchant is 0 ...... Finally, sort all the remainder in reverse order"

Excellent! The current business is 0.

After three computations, the remainder is 0, 1, and 1 respectively. All the remainder is sorted in reverse order, that is, 110!

6. Convert to binary. The result is 110.

Change the preceding section to a table:

Divisor |
Computing process |
Vendors |
Remainder |

6 |
6/2 |
3 |
0 |

3 |
3/2 |
1 |
1 |

1 |
1/2 |
0 |
1 |

(In a computer, tokens are expressed)

During the examination, it would be a little time-consuming to draw such a table. The more common conversion process is the Division:

(Figure: 1)

Compare the graph, table, and text description, and calculate how to convert 6 to binary number.

After talking about it for half a day, are our conversion Results Correct? Is the binary number 110 6? You have learned how to convert a binary number to a 10-digit number. Therefore, calculate whether the value of 110 is 6 if it is 10.

6.3.2 convert 10 hexadecimal numbers to 8 and 16 hexadecimal numbers

Very happy. The method for converting a 10-digit number to an 8-digit number is similar to the method for converting to a 2-digit number. The only change is that the divisor is changed from 2 to 8.

Let's look at an example of how to convert the decimal number 120 to the octal number.

Table representation:

Divisor |
Computing process |
Vendors |
Remainder |

120 |
120/8 |
15 |
0 |

15 |
15/8 |
1 |
7 |

1 |
1/8 |
0 |
1 |

120 is converted to octal, and the result is: 170.

Very happy. The method for converting a 10-digit number to a 16-digit number is similar to the method for converting to a 2-digit number. The only change is that the divisor is changed from 2 to 16.

It is also 120, and The hexadecimal format is:

Divisor |
Computing process |
Vendors |
Remainder |

120 |
120/16 |
7 |
8 |

7 |
7/16 |
0 |
7 |

120 is converted to hexadecimal with the result of 78.

Take pen paper and use the form (figure: 1) to calculate the process of the above two tables.

6.4 hexadecimal Conversions

The conversion between binary and hexadecimal is important. However, there is no need to calculate the conversion between the two. Every C, C ++ programmer can see the binary number and convert it directly to the hexadecimal number.

The same is true if we finish this section.

First, let's look at a binary number: 1111. What is it?

You may need to calculate as follows: 1*20 + 1*21 + 1*22 + 1*23 = 1*1 + 1*2 + 1*4 + 1*8 = 15.

However, since 1111 is only 4 bits, we must remember the weights of each bits, and remember them from high to low: 8, 4, 2, and 1. That is, the maximum bit is 23 = 8, followed by 22 = 4, 21 = 2, 20 = 1.

Remember 8421. For any 4-bit binary number, we can quickly calculate the corresponding 10-digit value.

The following lists all possible values of the four-digit binary xxxx (skipped in the middle)

A four-digit binary number is used to calculate the hexadecimal value of a decimal number.

1111 = 8 + 4 + 2 + 1 = 15 f

1110 = 8 + 4 + 2 + 0 = 14 E

1101 = 8 + 4 + 0 + 1 = 13 d

1100 = 8 + 4 + 0 + 0 = 12 C

1011 = 8 + 4 + 0 + 1 = 11 B

1010 = 8 + 0 + 2 + 0 = 10

1001 = 8 + 0 + 0 + 1 = 10 9

....

0001 = 0 + 0 + 0 + 1 = 1 1

0000 = 0 + 0 + 0 + 0 = 0 0

**To convert a binary number to a hexadecimal value, the binary number is converted to a hexadecimal value in four digits.**

For example ):

1111 1101,101 0 0101,100 1 1011

F d, a 5, 9 B

When we see FD, how can we quickly convert it to a binary number?

First convert F:

When we see F, we need to know that it is 15 (maybe you are not familiar with ~ F). How can we use 8421 for 15? It should be 8 + 4 + 2 + 1, so the four digits are all 1: 1111.

Then convert D:

When I see D, how can I use 8421 to get it together? It should be: 8 + 2 + 1, that is: 1011.

Therefore, FD is converted to the binary number, which is 1111 1011.

Because the hexadecimal conversion to binary is quite direct, when we need to convert a decimal number to a binary number, we can also convert it to a hexadecimal number first, then convert it to a binary system.

For example, to convert a decimal number 1234 to a binary number, if you want to divide it by 2 all the time, you need to calculate a large number of times. So we can divide it by 16 to get the hexadecimal number:

Divisor |
Computing process |
Vendors |
Remainder |

1234 |
1234/16 |
77 |
2 |

77 |
77/16 |
4 |
13 (d) |

4 |
4/16 |
0 |
4 |

Result: The hexadecimal value is 0x4d2.

Then we can directly write the binary format 0x4d2: 0100 1011 0010.

The ing relationship is:

0100 -- 4

1011 -- d

0010 -- 2

Similarly, if a binary number is very long, we need to convert it into a 10-digit number, except for the previous method, we can also convert the binary to hexadecimal and then to hexadecimal.

The following is an example of the binary number of the int type:

01101101 11100101 10101111 00011011

We convert the four-digit group to hexadecimal: 6d E5 af 1b

SELF: http://hi.baidu.com/0204_go/blog/item/af2717f4fedf4a2f720eecc7.html