c - 如何在char中保存一个int值

标签 c ascii

我有小于 255 的整数,想将它们保存到一个字符数组中。我正在尝试一些测试:

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

int
main(void)
{
    char string[10];
    int integers[10]; //all under 255.

    int j;
    for(j=0; j<10; j++)
    {
            integers[j]=j;
    }

    int i;
    for(i=0; i<10; i++)
    {
            string[i]=(char)integers[i];
    }

    printf("%s \n", string);
    return 0;
}

当我将调试器运行到程序末尾时,字符串包含以下 ascii 值:

"\000\001\002\003\004\005\006\a\b\t"

首先 我不明白为什么在006 之后会出现一个\a 而在末尾会出现一个\t?

其次 我想知道是否有更好的方法来实现我的意图?谢谢

最佳答案

您看到的是 ASCII 的转义表示字符 0x06、0x07、0x08 和 0x09。

0x06 是ACK

0x07 是 BEL\a(警报),只会导致终端“叮!”

0x08 是 BS 或退格键或 \b

0x09 是 TAB\t

关于c - 如何在char中保存一个int值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13399935/

相关文章:

java - 删除用于简单字母表的 ascii

c - 我可以打印出我读到的字符串值,但在转换为使用其 ASCII 等效项时遇到问题

python - 如何在 Python 中使用 MinGW-w64 构建我的 C 扩展?

c - 如果函数参数超出范围,则强制编译错误

c++ - 代码块无法在 mac OSX 上编译

html - vs Α 在浏览器和源代码中显示

c++ - 扩展 ASCII 字符的十进制值

c - 用 C 打印素数

c - 用 C 编写的嵌入式系统中的返回代码值

java - 有效/无效的非 ASCII 字符和无效的 ASCII 字符