为什么将“0"添加到 int 数字允许转换为 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.
推荐答案
如果你看一下 ASCII 表,asciitable,你'会看到数字从 48 开始(为 '0')到 57(为 '9').所以为了得到一个数字的字符代码,你可以将该数字添加到字符代码'0'中.
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'.
相关文章