转换字符在C和C为int ++ [英] Convert char to int in C and C++
本文介绍了转换字符在C和C为int ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何转换字符
到 INT
C和C ++?
How do I convert a char
to an int
in C and C++?
推荐答案
取决于你想要做什么:
读取值作为ASCII code,你可以写
to read the value as an ascii code, you can write
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 */
这篇关于转换字符在C和C为int ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文