将char []转换为Int []

时间:2014-10-07 14:32:45

标签: c

我有一个像这样的字符数组:

char res[80]="00001111001010100100001010101100000010011100001000110010000101000100001010010001"

想要改成

int res[80]=00001111001010100100001010101100000010011100001000110010000101000100001010010001

做了类似的事情:

char res[80]="00001111001010100100001010101100000010011100001000110010000101000100001010010001";
int resInt[80];
for (int i = 0; i<80; i++) {
    resInt [i] = res[i];
    printf("%d",resInt[i]);
}

结果如下:

4848484849494949484849484948494848494848484849484948494849494848484848484948484949494848484849484848494948484948484848494849484848494848484849484948484948484849
谁能帮助我? 感谢

2 个答案:

答案 0 :(得分:7)

ASCII'0'实际上是标量形式的48,所以减去它:

resInt[i] = res[i] - '0';

答案 1 :(得分:0)

字符文字在C中的类型为int。只需分配给int即可转换char类型。但是,生成的int值将是ASCII值。所以你需要减去char&#39; 0&#39;的ASCII值。获得实际价值。

resInt [i] = res [i] - &#39; 0&#39;;