2014-10-07 76 views
0

我有一个char数组是这样的:转换的char []到INT []

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 

任何人都可以帮助我? 感谢

回答

7

ASCII“0”其实48是标量的形式,所以减出来:

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

非常感谢,你救了我的生命.... :) – 2014-10-07 15:01:46

+1

@BakhtiarYusuf,而不是张贴“谢谢!” ,接受答案,因为它解决了你的问题 – 2014-10-07 15:33:46

0

字符文字是在C int类型,你可以简单地将其分配给一个int,char型转换。但是,生成的int值将是ASCII值。所以你需要减去char'0'的ASCII值来获得实际值。

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