Why does adding a #39;0#39; to an int digit allow conversion to a char?(为什么将“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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么将“0"添加到 int 数字允许转换为 char?


基础教程推荐
- 常量变量在标题中不起作用 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 我有静态或动态 boost 库吗? 2021-01-01