发布日期:2018-03-26
在C和C++中将字符串类型转换为整型+ 查看更多
在C和C++中将字符串类型转换为整型
+ 查看更多
发布日期:2018-02-26 14:34
分类:CPlusPlus
浏览次数:115
在C和C++中,我怎么样可以把char类型转换为int类型呢?
回答
这取决于你想做什么:如果你想读取这个值的ASCII码,你可以这样写:
char a = 'a'; int ia = (int)a; /* note that the int cast is not necessary -- int ia = a would suffice */如果你想像这样“'0’->0 , ‘1’->1”转换字符,你可以这样写:
char a = '4'; int ia = a - '0'; /* check here if ia is bounded by 0 and 9 */