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?
基础教程推荐
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- c++ STL设置差异 2022-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- 提升 ASIO 流缓冲 2021-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
