简体   繁体   English

为什么在整数位数后加“ 0”可以转换为字符?

[英]Why does adding a '0' to an int digit allow conversion to a char?

I've seen examples of this all over the place: 我到处都看到了这样的例子:

int i = 2;
char c = i + '0';
string s;
s += char(i + '0');

However, I have not yet seen an explanation for why adding the zero allows for the conversion. 但是,我还没有看到为什么加零允许转换的解释。

If you look at the ASCII table, asciitable , you'll see that the digits start at 48 (being '0') and go up to 57 (for '9'). 如果查看ASCII表asciitable ,您会看到数字从48开始(为“ 0”),然后上升为57(对于“ 9”)。 So in order to get the character code for a digit, you can add that digit to the character code of '0'. 因此,为了获取数字的字符代码,可以将该数字添加到字符代码“ 0”。

When ASCII encoding is used, the integer value of '0' is 48 . 使用ASCII编码时,整数值'0'48

'0' + 1 = 49 = '1'
'0' + 2 = 50 = '2'

...

'0' + 9 = 57 = '9'

So, if you wanted convert a digit to its corresponding character, just add '0' to it. 因此,如果要将数字转换为相应的字符,只需在其上添加'0'即可。

Even if the platfrom uses non-ASCII encoding, the lanuage still guarantees that the characters '0' - '9' must be encoded such that: 即使platfrom使用非ASCII编码,该语言仍然保证必须对字符'0' - '9'进行编码,使得:

'1' - '0' = 1
'2' - '0' = 2
'3' - '0' = 3
'4' - '0' = 4
'5' - '0' = 5
'6' - '0' = 6
'7' - '0' = 7
'8' - '0' = 8
'9' - '0' = 9

When ASCII encoding is used, that becomes: 当使用ASCII编码时,将变为:

'1' - '0' = 49 - 48 = 1
'2' - '0' = 50 - 48 = 2
'3' - '0' = 51 - 48 = 3
'4' - '0' = 52 - 48 = 4
'5' - '0' = 53 - 48 = 5
'6' - '0' = 54 - 48 = 6
'7' - '0' = 55 - 48 = 7
'8' - '0' = 56 - 48 = 8
'9' - '0' = 57 - 48 = 9

Hence, regardless of the character encoding used by a platform, the lines 因此,无论平台使用哪种字符编码,

int i = 2;
char c = i + '0';

will always result in the value of c being equal to the character '2' . 将始终导致c的值等于字符'2'

The C++ standard says, in its [lex.charset] section “the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous” (quoted from draft N4659). C ++标准在其[lex.charset]部分中表示“上述十进制数字列表中的每个0后面的字符的值应比前一个的值大一个”(引自N4659草案)。

Thus, the value of '1' is ' 0'+1 , and the value of '2' is one more than that, which is '0'+2 , and so on. 因此, '1'的值是“ 0'+1 ,而'2'的值则比'0'+2 ,依此类推。 If n has a value from 0 to 9, then '0'+n is the value for the corresponding character from '0' to '9' . 如果n的值介于0到9之间,则'0'+n是从'0''9'的对应字符的值。

(Unlike the earlier answers, this answer does not assume ASCII and shows how the property derives from the C++ standard.) (与以前的答案不同,此答案不采用ASCII,而是显示该属性如何从C ++标准派生。)

It's based on ASCII values. 它基于ASCII值。 Adding the ASCII value of 0 which is 48 means that 48 + 5 will be 53 or ASCII 53 which is 5. 将ASCII值0加到48表示48 + 5将是53或ASCII 53则是5。

Google ASCII and find a good chart and study it. Google ASCII并找到一个好的图表并进行研究。 It should make sense once you look at the values for each char (character). 一旦查看了每个字符(字符)的值,它就应该有意义。

(char)(i+c) where c is a char gives you a new char with an ascii value equal to (ascii value of c ) + i . (char)(i+c)其中c是一个char会为您提供一个新的char ,其ascii值等于( c ascii值)+ i Since the ascii values of the digits 0-9 are sequential, i+'0' gives you the char corresponding to i , as long as i lies in the appropriate range. 由于数字0-9的ascii值是连续的,因此只要i处于适当的范围内, i+'0'为您提供与i对应的字符。

EDIT : (i+c) is an int, as Jarod42 pointed out. 编辑: (i+c)是一个整数,正如Jarod42指出的那样。 Added a cast to maintain correctness. 添加了强制转换以保持正确性。

字符将根据ASCII值将整数转换为相应的值,因此,如果将48分配给字符,则它将存储0,因为48是ASCII值0.相应地,将ASCII值0添加到任何整数您需要转换为char

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM