您的位置:首页 > 其它

int and char conversion

2014-10-01 21:52 295 查看
http://stackoverflow.com/questions/1114741/how-to-convert-int-to-char-c

Are you looking for the char equivalent of a single digit number? For example, converting 5 to '5'? If so then you can do the following, assuming of course the char is 9 or less.

char dig = (char)(((int)'0')+i);

http://stackoverflow.com/questions/5029840/convert-char-to-int-in-c-and-c
Depends on what you want to do:

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 */

to convert the character
'0' -> 0
,
'1' -> 1
, etc, you can write

char a = '4';
int ia = a - '0';
/* check here if ia is bounded by 0 and 9 */
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: