C ++ hexadecimal conversion

Source: Internet
Author: User
Tags arabic numbers binary to decimal cmath

Ii. Convert hexadecimal to decimal

Convert binary to decimal: 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

Convert octal to decimal: In octal mode, the number of octal nodes increases from 8 to 1, and the number of octal nodes ranges from 0 to 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 1507 is converted to the decimal number 839.

Octal expression
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, C ++ specifies that if a number is specified to use octal, a 0 value must be added before it. For example, 123 is in decimal format, but 0123 indicates that octal is used. 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 100 of the octal value. We will learn how to convert a 10-digit number into an octal value.
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.


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.

Convert hexadecimal to decimal 
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. Therefore, we use the letters A, B, C, D, E, and F to represent 10, 11, 12, 13, 14, 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

Hexadecimal expression
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 ++ requires that the hexadecimal number must start with 0x. For example, 0x1 indicates a hexadecimal number, while 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.

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.


Convert decimal number to binary, octal, and hexadecimal number

Decimal 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, 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.
That is: 1, 2, get the quotient is 0, the remainder is 1 (take the pen paper for calculation, 1, 2 is not the quotient 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:
Formula Remainder
6 6/2 3 0
3 3/2 1 1
1 1/2 0 1

10-in-hexadecimal number to 8 or 16-in-hexadecimal number 
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:
Formula 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:
Formula Remainder
120 120/16 7 8
7 7/16 0 7
120 is converted to hexadecimal with the result of 78.

Hexadecimal number Conversion 
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:
Formula 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

Source code, reverse code, and supplementary code

For example, suppose there is a number of int type, the value is 5, then we know that it is represented in the computer:
00000000 00000000 00000000 00000101
5 is converted to a binary system of 101, but the number of int types occupies 4 bytes (32 bits), so a bunch of 0 values are filled in front.
Now I want to know how-5 is represented in a computer?
In a computer, a negative number is expressed in the form of a positive complement.
What is a supplemental code? This should start with the original code and the reverse code.
Original code: an integer that is converted into binary numbers based on the absolute value. It is called the original code.
For example, 00000000 00000000 00000000 00000101 is the source code of 5.
Reverse code: returns the bitwise result of the binary number. The new binary number obtained is called the reverse code of the original binary number.
The reverse operation indicates that the original value is 1, and the value is 0. The original value is 0, and the value is 1. (1 to 0; 0 to 1)
For example, if the bitwise of 00000000 00000000 00000000 is reversed, 00000101 11111111 11111111 is obtained.
Said: 11111111 11111111 11111111 11111010 00000000 is an anti-code of 00000000 00000000 00000101.
The anti-code is mutual, so it can also be called:
11111111, 11111111, 11111111, 11111010, 00000000, 00000000, 00000000, 00000101, and are mutually inverse codes.
Complement: the anti-code plus 1 is called a complement.
That is to say, to get a complement of a number, first obtain the reverse code, and then add 1 to the reverse code. The resulting number is called a complement.
For example, the 00000000 00000000 00000000 00000101 anti-code is: 11111111 11111111 11111111 11111010.
Then, the complement code is:
11111111 11111111 11111111 11111010 + 1 = 11111111 11111111 11111111
Therefore,-5 is expressed in the computer as 11111111 11111111 11111111 11111011. Convert to hexadecimal: 0 xfffffffb.

Let's look at how integer-1 is represented in a computer.
Suppose this is also an int type, then:
1. first obtain the original code of 1: 00000000 00000000 00000000 00000001
2. Reverse code: 11111111 11111111 11111111 11111110
3. Supplemental code: 11111111 11111111 11111111 11111111
It can be seen that the Binary Expression of-1 in a computer is full 1. Hexadecimal: 0 xffffff.

Decimals must be multiplied, for example, 0.2. decimals generally require precision.
This section describes the binary conversion of 6-bit precision.
0.2*2 (in decimal order, multiply by a number, and in hexadecimal order, multiply by 8) = 0.4 use the number 0 before the decimal point
0.4*2 = 0.8 0
0.8*2 = 1.6 get 1 (if the product is greater than 1, use the number following the decimal point to continue multiplication)
0.6*2 = 1.2 get 1
0.2*2 = 0.4 0
0.4*2 = 0.8 0
The result is to sort the number from top to bottom, that is, 0.001100.

Code: Convert octal to decimal

#include "stdafx.h"#include<cmath>#include<cstdio>#include<iostream>using namespace std;#define BIT_MASK(bit_pos) (0x01<<bit_pos)int change(int val){int temp=0,result=0,index=0;while(val>0){temp=val%10;val/=10;result+=temp*static_cast<int>(pow(8,index++));}return result;}int _tmain(int argc, _TCHAR* argv[]){int num;cout<<"Please input an oct number.(Besure the number you input is begin with a '0')"<<endl;cin>>num;cout<<change(num)<<endl;system("pause");return 0;}

Convert X to decimal

# Include "stdafx. H "# include <cmath> # include <cstdio> # include <iostream> using namespace STD; # define bit_mask (bit_pos) (0x01 <bit_pos) int change (INT Val, int X) {int temp = 0, result = 0, Index = 0; while (Val> 0) {temp = Val % 10; val/= 10; Result + = temp * static_cast <int> (POW (x, index ++);} return result;} int _ tmain (INT argc, _ tchar * argv []) {int num, X; cout <"input conversion hexadecimal (, 16), CTRL + Z to end:"; CIN> X; cout <"Please input an OCT number. (CTRL + Z to end) "<Endl; CIN> num; cout <x <" base number "<num <" to convert to decimal: "<change (Num, x) <Endl; System (" pause "); Return 0 ;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.