从char转换为整数值 [英] Converting from char to integer value

查看:94
本文介绍了从char转换为整数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的程序中,为了降低复杂性,整数存储为char,

因此2存储为2,即十进制:50

我可以在程序中获得''2'的整数值,

即我想将''2'处理为int:2,而不是int:50?


对不起这个新手问题。

解决方案

" hantie" <公顷**** @ hotmail.com>写道:

在我的程序中,为了降低复杂性,整数存储为char,
因此2存储为2,即十进制:50
我可以在程序中获得''2'的整数值,
即我想将''2'处理为int:2,而不是int:50?




c - ''0''


这是有效的,因为数字字符的字符编码是

保证连续且顺序正确。因此,如果

'''''字符的值为50,则'0''保证在同一系统上具有值48.




Martin


文章< 7h ******************* @ news -server.bigpond.net.au>,
ha****@hotmail.com 说... < blockquote class =post_quotes>在我的程序中,为了降低复杂性,整数存储为char,
因此2存储为2,即十进制:50
是可能的为了让我在程序中获得''2'的整数值,
即我想要处理''2'作为int:2,而不是int:50?

抱歉对于这个新手问题。




stdlib.h中的strtol()函数可能正是你要找的。


-

Randy Howard _o

2reply删除FOOBAR \<,

______________________()/()___________________________________ ___________

SCO Spam-magnet: po ******** @ sco.com


hantie写道:


在我的程序中,为了降低复杂性,整数存储为char,
所以2存储为''2'',这是十进制的:50
我可以在程序中获得''2'的整数值,
即我想要处理''2'作为int:2,而不是int:50?

抱歉这个新手问题。




字符代码对于''0''到''9''保证

是连续的,所以你可以减去:


char two_as_char =''2 '';

int two_as_int = two_as_char - ''0'';


-
Er ********* @ sun.com


In my program, for reducing the complexity, the integer was stored as char,
so 2 is stored as ''2'', which is decimal: 50
Is is possible for me to obtain the integer value of ''2'' in the program,
i.e. I want to process ''2'' as int: 2, not int: 50 ?

Sorry for this newbie question.

解决方案

"hantie" <ha****@hotmail.com> writes:

In my program, for reducing the complexity, the integer was stored as char,
so 2 is stored as ''2'', which is decimal: 50
Is is possible for me to obtain the integer value of ''2'' in the program,
i.e. I want to process ''2'' as int: 2, not int: 50 ?



c - ''0''

This works because the character encodings of the digit characters are
guaranteed to be consecutive and in the correct order. Therefore, if
the ''2'' character has value 50, ''0'' is guaranteed to have value 48 on
the same system.

Martin


In article <7h*******************@news-server.bigpond.net.au>,
ha****@hotmail.com says...

In my program, for reducing the complexity, the integer was stored as char,
so 2 is stored as ''2'', which is decimal: 50
Is is possible for me to obtain the integer value of ''2'' in the program,
i.e. I want to process ''2'' as int: 2, not int: 50 ?

Sorry for this newbie question.



The strtol() function from stdlib.h might be what you''re looking for.

--
Randy Howard _o
2reply remove FOOBAR \<,
______________________()/ ()______________________________________________
SCO Spam-magnet: po********@sco.com


hantie wrote:


In my program, for reducing the complexity, the integer was stored as char,
so 2 is stored as ''2'', which is decimal: 50
Is is possible for me to obtain the integer value of ''2'' in the program,
i.e. I want to process ''2'' as int: 2, not int: 50 ?

Sorry for this newbie question.



The character codes for ''0'' through ''9'' are guaranteed
to be consecutive, so you can just subtract:

char two_as_char = ''2'';
int two_as_int = two_as_char - ''0'';

--
Er*********@sun.com


这篇关于从char转换为整数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆