在C或C ++中,字符值存储为ASCII值。要将int转换为ASCII,我们可以将字符'0'的ASCII与整数相加。让我们看一个将int转换为ASCII值的示例。
#include<stdio.h>
int intToAscii(int number) {
return '0' + number;
}
main() {
printf("The ASCII of 5 is %d\n", intToAscii(5));
printf("The ASCII of 8 is %d\n", intToAscii(8));
}输出结果
The ASCII of 5 is 53 The ASCII of 8 is 56