为什么在 C 中减去“0”会导致字符所代表的数字?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15598698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:31:33  来源:igfitidea点击:

Why does subtracting '0' in C result in the number that the char is representing?

c++c

提问by Silviu.

Can someone explain why this works?

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

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

Why does subtracting '0' from an ascii code of a char result the number that that char is representing?

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

回答by Jean

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

因为字符都是用数字表示的,而 '0' 是第一个。

On the table below you see that:

在下表中,您可以看到:

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


'9' => 57.

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

其结果是:'9'- '0')=(57 48)= 9

enter image description hereSource: http://www.asciitable.com

在此处输入图片说明资料来源:http: //www.asciitable.com

回答by Joseph Mansfield

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

char是一个整数类型,就像int和家庭一样。类型对象char具有一些数值。您在字符文字(如'0')中键入的字符与char对象具有的值之间的映射由执行字符集中该字符的编码决定:

  • C++11 §2.14.3:

    An ordinary character literal that contains a single c-charrepresentable in the execution character set has type char, with value equal to the numerical value of the encoding of the c-charin 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 intcan be converted to a char.

  • C++11 §2.14.3:

    包含可在执行字符集中表示的单个c-char的普通字符文字具有 type char,其值等于执行字符集中c-char编码的数值。

  • C99 §6.4.4.4:

    整数字符常量是用单引号括起来的一个或多个多字节字符的序列,如'x'.

    [...]

    整数字符常量的类型为int

    请注意,int可以将 转换为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 needto be ASCII compatible. There are some restrictions, though. One of them is as follows (C++11 §2.3, C99 §5.2.1):

执行字符集的选择取决于实现。通常情况下,该选择与 ASCII 兼容,因此其他答案中发布的表格具有适当的值。但是,字符集不需要与 ASCII 兼容。不过也有一些限制。其中之一如下(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.

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
_ { } [ ] # ( ) < > % : ; . ? * + - / ^ & | ~ ! = , \ " '

[...]

在源和执行基本字符集中,上述十进制数字列表中 0 之后的每个字符的值都应比前面的值大 1。

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:

这意味着无论字符'0'具有什么值,字符的'1'值都比 大 1 '0',而字符的'2'值又比 大一,以此类推。数字字符具有连续值。您可以像这样总结映射:

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'.

所有数字字符的值都从 的值偏移'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.

这意味着,如果你有一个字符,让我们说'9'并减去'0'它,你会得到执行字符集中的值'9'和值之间的“距离” '0'。由于它们是连续的,因此距离将为 9。

回答by Silviu.

Because the C standard guarantees that the characters 0, 1, 2, 3, 4, 5, 6, 7, 8, 9are 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...

因为 C 标准保证字符0, 1, 2, 3, 4, 5, 6, 7, 8, 9始终按其数字字符代码的顺序排列。所以,如果你'0'从另一个数字中减去 的字符代码,它将给出它相对于 的位置0,这是它的值......

From the C standard, Section 5.2.1 Character sets:

从 C 标准,第 5.2.1 节字符集:

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

在源和执行基本字符集中,上述十进制数字列表中 0 之后的每个字符的值都应比前面的值大 1

回答by Aniket Inge

Because, the literals are arranged in sequence.

因为,文字是按顺序排列的。

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

因此,如果0是 48,1将是 49,2将是 50 等等。在 ASCII 中,那么x将包含,ascii 值'9'减去 ascii 值,'0'这意味着,ascii 值'9'将是 57,因此,x将包含57 - 48 = 9.

Also, charis an integral type.

此外,char是一个整数类型。

回答by MOHAMED

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

数字字符的代码 ascii'0' '1' '2' '3' '4' '5' '6' '7' '8' '9'按照ascii 表中指示的顺序排序

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

因此,如果我们在 asii of'9'和 ascii of 之间进行区分,'0'我们将得到9

回答by bash.d

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, 9has value 57and 0has 48, so if you subtract 48 from 57 you get 9. Just have a look at the ASCII-table.

在 ASCII 表中,数字按顺序排列,从 的最低代码开始0。如果从 中减去更大的数字0,则会创建两个 ASCII 值的差值。所以,9has value570has 48,所以如果你从 57 中减去 48 你得到 9。看看 ASCII 表。

Look here.

这里

回答by Thanakron Tandavas

Look at the ASCII TABLE:

查看ASCII 表

'9' in ASCII =  57 //in Decimal

'0' in ASCII =  48 //in Decimal

57 - 48 = 9

57 - 48 = 9

回答by Wolfgang Skyler

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.

字符以文本形式表示数字,但作为数字时具有不同的值。Windows 使用数字来决定要打印的字符。所以数字 0x30 代表 windows 操作系统中的字符 0。数字 0x39 代表字符 9。毕竟计算机只能识别数字,它不知道“字符”是什么。

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

不幸的是,(int)('f' - '0')它不等于 15,不过。

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

这为您提供了各种字符和用于表示它们的数字窗口。 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.

如果您需要为另一个操作系统找到它,您可以搜索:Virtual Key Codes <OSname>在 Google 中。查看其他操作系统的代码。