在 C/C++ 中将 int 转换为 ASCII 字符


CC++ 中,字符值存储为 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

更新于:2023 - 10 - 05

33K+ 浏览量

开启您的职业生涯

完成课程获得认证

开始
广告
© . All rights reserved.