char [0-9]到int [0-9] [英] char [0-9] to int [0-9]

查看:184
本文介绍了char [0-9]到int [0-9]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种优雅的方式来做一些像


//给出一个char {0,1,2,3,5,5,7,8,9}将它作为简短的返回

短char2digit(char c)

{

短d = c-48;

返回d;

}





谢谢。

Is there an elegant way to do something like

// given a char {0,1,2,3,4,5,6,7,8,9} returns it as a short
short char2digit(char c)
{
short d = c-48;
return d;
}

?

Thanks.

推荐答案



Gaijinco写道:

Gaijinco wrote:
是否有一种优雅的方式来做类似的事情
//给出一个字符{0,1,2,3,4,5,6,7,8,9}将其作为简短的返回它返回char2digit(char c)
{
短d = c-48;


d = c - ''0'';


是便携式解决方案。

返回d;
}
Is there an elegant way to do something like

// given a char {0,1,2,3,4,5,6,7,8,9} returns it as a short
short char2digit(char c)
{
short d = c-48;
d = c - ''0'';

is the portable solution.
return d;
}




Tobias



Tobias


Gaijinco写道:
Gaijinco wrote:
是否有一种优雅的方式来做一些像

//给出一个char {0,1,2,3,4,5,6,7,8,9}


我假设你的意思是''0'等。字符类型是一个整数类型,可以实际上将
设置为那些整数值。 ''''与

字符文字一起使用以区分它。

将其作为简短的返回来短信char2digit(char c)
{
短d = c-48;
返回d;
}

Is there an elegant way to do something like

// given a char {0,1,2,3,4,5,6,7,8,9}
I assume you mean ''0'' etc. The char type is an integer type and could
actually be set to those integral values. The '''' are used with
character literals to differentiate that.
returns it as a short
short char2digit(char c)
{
short d = c-48;
return d;
}

?




'不可移植,因为它采用ASCII字符集。

的方法是:


short char2digit(char c)

{

返回c - ''0'';

}

你的变量d是不需要的,但没有害处。


Brian



That''s not portable, as it assumes the ASCII character set. The way to
do it is:

short char2digit(char c)
{
return c-''0'';
}
Your variable d was unneeded, but not harmful.

Brian


******* @ hotmail.com 写道:
Gaijinco写道:
Gaijinco wrote:
是否有一种优雅的方式来做一些像

//给出一个char {0,1,2 ,3,4,5,6,7,8,9}返回它作为简短的char2digit(char c)
{
短d = c-48;
Is there an elegant way to do something like

// given a char {0,1,2,3,4,5,6,7,8,9} returns it as a short
short char2digit(char c)
{
short d = c-48;



d = c - ''0'';

是便携式解决方案。



d = c - ''0'';

is the portable solution.




是的,和这是真的,因为标准特别保证

数字''''到''9''具有连续且按递增顺序连续的表示。请记住,对于其他字符没有

这样的保证,例如大写或小写字母

字母(并且字符集中没有字符集) />
连续)。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



Yes, and this true only because the standard specifically guarantees
that the digits ''0'' through ''9'' have representations that are
consecutive and in increasing order. Keep in mind that there is no
such guarantee for other characters, such as upper or lower case
letters (and there are character sets in which the letters are not
contiguous).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


这篇关于char [0-9]到int [0-9]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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