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

查看:19
本文介绍了为什么将“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'.

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

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