如何转换char到C的整数? [英] How to convert char to integer in C?
本文介绍了如何转换char到C的整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能的重复项:
如何将单个字符转换为int
字符到C中的整数
任何机构都可以告诉我如何将 char
转换为 int
?
char c [] = {'1',':','3'};
int i = int(c [0]);
printf(%d,i);
当我尝试这个时,它会给出49。
解决方案在过去,我们可以假设大多数计算机使用ASCII,我们只需要执行
int i = c [0] - '0';
但是在这些日子里,Unicode并不是一个好主意。如果你的代码必须在非ASCII计算机上运行,这从来不是一个好主意。
编辑:虽然看起来很疯狂,是标准保证工作。感谢@Earwicker。
Possible Duplicates:
How to convert a single char into an int
Character to integer in CCan any body tell me how to convert a
char
toint
?char c[]={'1',':','3'}; int i=int(c[0]); printf("%d",i);
When I try this it gives 49.
解决方案In the old days, when we could assume that most computers used ASCII, we would just do
int i = c[0] - '0';
But in these days of Unicode, it's not a good idea. It was never a good idea if your code had to run on a non-ASCII computer.Edit: Although it looks hackish, evidently it is guaranteed by the standard to work. Thanks @Earwicker.
这篇关于如何转换char到C的整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文