33

有人可以解释为什么这有效吗?

char c = '9';
int x = (int)(c - '0');

为什么从 char 的 ascii 代码中减去“0”会得到该 char 所代表的数字?

4

8 回答 8

50

Because the char are all represented by a number and '0' is the first of them all.

On the table below you see that:

'0' => 48
'1' => 49


'9' => 57.

As a result: ('9' - '0') = (57 − 48) = 9

enter image description here Source: http://www.asciitable.com

于 2013-03-24T12:55:59.417 回答
29

char is an integer type, just like int and family. An object of type char has some numerical value. The mapping between characters that you type in a character literal (like '0') and the value that the char object has is determined by the encoding of that character in the execution character set:

  • C++11 §2.14.3:

    An ordinary character literal that contains a single c-char representable in the execution character set has type char, with value equal to the numerical value of the encoding of the c-char in the execution character set.

  • C99 §6.4.4.4:

    An integer character constant is a sequence of one or more multibyte characters enclosed in single-quotes, as in 'x'.

    [...]

    An integer character constant has type int.

    Note that the int can be converted to a char.

The choice of execution character set is up to the implementation. More often than not, the choice is ASCII compatible, so the tables posted in other answers have the appropriate values. However, the character set does not need to be ASCII compatible. There are some restrictions, though. One of them is as follows (C++11 §2.3, C99 §5.2.1):

a b c d e f g h i j k l m n o p q r s t u v w x y z
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
0 1 2 3 4 5 6 7 8 9
_ { } [ ] # ( ) < > % : ; . ? * + - / ^ & | ~ ! = , \ " ’

[...]

In both the source and execution basic character sets, the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous.

This means that whatever value the character '0' has, the character '1' has value one more than '0', and character '2' has value one more than that, and so on. The numeric characters have consecutive values. You can summarise the mapping like so:

Character:            0    1    2    3    4    5    6    7    8    9
Corresponding value:  X    X+1  X+2  X+3  X+4  X+5  X+6  X+7  X+8  X+9

All of the digit characters have values offset from the value of '0'.

That means, if you have a character, let's say '9' and subtract '0' from it, you get the "distance" between the value of '9' and the value of '0' in the execution character set. Since they are consecutive, the distance will be 9.

于 2013-03-24T13:00:02.660 回答
22

Because the C standard guarantees that the characters 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 are always in this order regarding their numerical character code. So, if you subtract the char code of '0' from another digit, it will give its position relative to 0, which is its value...

From the C standard, Section 5.2.1 Character sets:

In both the source and execution basic character sets, the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous

于 2013-03-24T12:56:53.077 回答
5

Because, the literals are arranged in sequence.

So if 0 was 48, 1 will be 49, 2 will be 50 etc.. in ASCII, then x would contain, ascii value of '9' minus the ascii value of '0' which means, ascii value of '9' would be 57 and hence, x would contain 57 - 48 = 9.

Also, char is an integral type.

于 2013-03-24T12:56:12.617 回答
3

the code ascii of numeric chars are ordered in the order '0' '1' '2' '3' '4' '5' '6' '7' '8' '9' as indicated in the ascii table

so if we make difference beween asii of '9' and ascii of '0' we will get 9

于 2013-03-24T12:56:41.647 回答
3

In the ASCII-table the Digits are aligned sequentially, starting with the lowest code for 0. If you subtract a higher number from 0, you create the difference of the two ASCII-values. So, 9 has value 57 and 0 has 48, so if you subtract 48 from 57 you get 9. Just have a look at the ASCII-table.

Look here.

于 2013-03-24T12:56:56.427 回答
2

Look at the ASCII TABLE:

'9' in ASCII =  57 //in Decimal

'0' in ASCII =  48 //in Decimal

57 - 48 = 9

于 2013-03-24T12:57:29.183 回答
-1

First, try:

cout << (int)'0' << endl;

now try:

cout << (int)'9' << endl;

the charictors represent numbers in text form, but have a different value in when taken as a number. Windows uses a Number to decide which charictor to print. So the number 0x30 represents the charictor 0 in the windows OS. The number 0x39 represents the charictor 9. After all, all a computer can recognize is numbers, it does'nt know what a "char" is.

Unfortunatly (int)('f' - '0') does not equal 15, though.

This gives you the various charictors and the number windows uses to represent them. http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx

If you need to find that for another OS, you can search: Virtual Key Codes <OSname> in Google. to see what other OS's have as their codes.

于 2013-03-24T13:04:03.013 回答