为什么在int数字中添加'0'允许转换为char? [英] Why does adding a '0' to an int digit allow conversion to a char?

查看:171
本文介绍了为什么在int数字中添加'0'允许转换为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'.

这篇关于为什么在int数字中添加'0'允许转换为char?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆