<分区>
例如,我如何将小于 10 的 int 转换为 char :
5 -> '5'
(不使用 ASCII 表将 int 转换为 char)
<分区>
例如,我如何将小于 10 的 int 转换为 char :
5 -> '5'
(不使用 ASCII 表将 int 转换为 char)
最佳答案
由于数字在标准字符集中总是连续的,您可以这样写:
int number = 5;
int character = number + '0';
/* Here, character == '5' */
例如,参见 C11 标准。
n1570, § 5.2.1 Character sets
The 10 decimal digits:
0 1 2 3 4 5 6 7 8 9
[...]
In both the source and execution basic character sets, the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous.
这同样适用于 C++。
n3337, § 2.3 Character sets
In both the source and execution basic character sets, the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous.
关于c++ - 不使用 ASCII 将 int 转换为 char,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17320769/