如何在char中保存int值

pat*_*ues 0 c ascii

我有值为255的整数,并希望将它们保存到char数组中.我正在尝试一些测试:

#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;
}
Run Code Online (Sandbox Code Playgroud)

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

"\ 000\001\002\003\004\005\006\A\B\t" 的

首先我不明白为什么在006之后\ a出现并且最后是\ t?

其次我想知道是否有更好的方法来做我想要的事情?谢谢

Dan*_*umb 6

您所看到的是ASCII字符0x06,0x07,0x08和0x09的转义表示.

0x06是ACK.

0x07是BEL\a(警报),只是让终端去'叮!'

0x08是BS或退格或\b.

0x09是TAB\t.