将int转换为char而不使用ASCII [英] convert int to char without using ASCII
问题描述
如何将int转换为10以下的字符为例如:
5 - >'5'
(将int转换为char而不使用ASCII表格)
How can i convert an int below 10 to a char for example :
5 -> '5'
(convert int to char without using ASCII table)
推荐答案
由于数字在标准字符集中始终是连续的,因此您可以写为:
Since digits are always consecutive in a standard character set, you can write:
int number = 5;
int character = number + '0';
/* Here, character == '5' */
,例如C11标准。
n1570,第5.2.1节字符集
十进制数字:
0 1 2 3 4 5 6 7 8 9
[...]
在源和执行基本字符集中,上面的十进制数字列表中的每个字符的值应为0比以前的值。
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 ++。
The same applies in C++.
n3337,§2.3字符集
在源和执行基本字符集中,上述十进制数字列表中的每个字符在0之后的值应大于一个比上一个的值。
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.
这篇关于将int转换为char而不使用ASCII的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!