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

后端 未结 6 1161
星月不相逢
星月不相逢 2020-12-31 11:28

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

6条回答
  •  醉话见心
    2020-12-31 12:21

    It's based on ASCII values. Adding the ASCII value of 0 which is 48 means that 48 + 5 will be 53 or ASCII 53 which is 5.

    Google ASCII and find a good chart and study it. It should make sense once you look at the values for each char (character).

提交回复
热议问题