将字符串转换为int [英] Converting strings to int

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

问题描述

我想将数字(1234)的字符串表示转换为

int,并进行溢出和下溢检查。从本质上讲,我正在寻找一个strtol()来转换int而不是long的



strtol()的问题在于,一个适合长整数的数字可能太大而不适用于

一个int。 sscanf()似乎没有进行上溢/下溢检查。

atoi(),当然,没有做任何检查。我一直以为很奇怪



标准中没有strtoi()和朋友的int和short类型。


有什么建议吗?

I want to convert a string representation of a number ("1234") to an
int, with overflow and underflow checking. Essentially, I''m looking
for a strtol() that converts int instead of long. The problem with
strtol() is that a number that fits into a long might be too big for
an int. sscanf() doesn''t seem to do the over/underflow checking.
atoi(), of course, doesn''t do any checking. I''ve long thought it odd
that there aren''t strtoi() and friends for int and short types in the
standard.

Any suggestions?

推荐答案

al ******************* @ gmail.com 写道:

我想将数字(1234)的字符串表示转换为

int,并进行溢出和下溢检查。从本质上讲,我正在寻找一个strtol()来转换int而不是long的



strtol()的问题在于,一个适合长整数的数字可能太大而不适用于

一个int。 sscanf()似乎没有进行上溢/下溢检查。

atoi(),当然,没有做任何检查。我一直以为很奇怪



标准中没有strtoi()和朋友的int和short类型。


有什么建议吗?
I want to convert a string representation of a number ("1234") to an
int, with overflow and underflow checking. Essentially, I''m looking
for a strtol() that converts int instead of long. The problem with
strtol() is that a number that fits into a long might be too big for
an int. sscanf() doesn''t seem to do the over/underflow checking.
atoi(), of course, doesn''t do any checking. I''ve long thought it odd
that there aren''t strtoi() and friends for int and short types in the
standard.

Any suggestions?



使用strtol()并检查结果,看它是否适合int。


-

Ian Collins。

Use strtol() and check the result to see if it fits in an int.

--
Ian Collins.


5月24日上午12:22,allthecoolkids还有... @ gmail.com写道:
On May 24, 12:22 am, allthecoolkidshave...@gmail.com wrote:

我想将数字(1234)的字符串表示转换为

int,并进行溢出和下溢检查。
I want to convert a string representation of a number ("1234") to an
int, with overflow and underflow checking.



当然,30秒后,我想自己为什么不转换成

多,看看它是不是在INT_MIN和INT_MAX之间,如果是这样,返回

该值是否转换为int?"

Of course, 30 seconds later, I think to myself "Why not convert to a
long and see if it''s between INT_MIN and INT_MAX and if so return
that value casted to an int?"


人******************* @ gmail的。 com 写道:

我想将数字(1234)的字符串表示转换为

int ,溢出和下溢检查。从本质上讲,我正在寻找一个strtol()来转换int而不是long的



strtol()的问题在于,一个适合长整数的数字可能太大而不适用于

一个int。 sscanf()似乎没有进行上溢/下溢检查。

atoi(),当然,没有做任何检查。我一直以为很奇怪



标准中没有strtoi()和int和short类型的朋友。
I want to convert a string representation of a number ("1234") to an
int, with overflow and underflow checking. Essentially, I''m looking
for a strtol() that converts int instead of long. The problem with
strtol() is that a number that fits into a long might be too big for
an int. sscanf() doesn''t seem to do the over/underflow checking.
atoi(), of course, doesn''t do any checking. I''ve long thought it odd
that there aren''t strtoi() and friends for int and short types in the
standard.



检查INT_MAX和INT_MIN的长值。

然后你有值(如果转换为长期工作),即使是

的范围,以及你想要的错误检查。

Check the long value against INT_MAX and INT_MIN.
Then you have the value (if the conversion to long worked), even if out
of range for an int, and the error checking you want.


这篇关于将字符串转换为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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