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

后端 未结 6 1163
星月不相逢
星月不相逢 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:25

    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'). So in order to get the character code for a digit, you can add that digit to the character code of '0'.

提交回复
热议问题