C语言中如何将字符串转换为整数?
我试图找出是否有另一种方法可以在 C 中将字符串转换为整数。
我经常在代码中进行以下模式。
char s[] = "45";
int num = atoi(s);
那么,有没有更好的方法或者其他方法呢?
I am trying to find out if there is an alternative way of converting string to integer in C.
I regularly pattern the following in my code.
char s[] = "45";
int num = atoi(s);
So, is there a better way or another way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
在我看来,有
strtol
更好。我也喜欢strtonum
,因此如果有的话请使用它(但请记住它不可移植):您可能还对
strtoumax
和strtoimax
是 C99 中的标准函数。例如,您可以说:无论如何,远离
atoi
:There is
strtol
which is better IMO. Also I have taken a liking instrtonum
, so use it if you have it (but remember it's not portable):You might also be interested in
strtoumax
andstrtoimax
which are standard functions in C99. For example you could say:Anyway, stay away from
atoi
:基于
strtol
的稳健 C89 解决方案:
atoi
系列可能有)更严格的整数定义>strtol
(例如没有前导空格或尾随垃圾字符)GitHub 上游。
基于:https://stackoverflow.com/a/6154614/895245
Robust C89
strtol
-based solutionWith:
atoi
family)strtol
(e.g. no leading whitespace nor trailing trash chars)GitHub upstream.
Based on: https://stackoverflow.com/a/6154614/895245
不要使用
ato...
组中的函数。这些都已损坏且几乎无用。一个更好的解决方案是使用 sscanf,尽管它也不完美。要将字符串转换为整数,应使用
strto...
组中的函数。在您的具体情况下,它将是strtol
函数。Don't use functions from
ato...
group. These are broken and virtually useless. A moderately better solution would be to usesscanf
, although it is not perfect either.To convert string to integer, functions from
strto...
group should be used. In your specific case it would bestrtol
function.您可以编写
atoi()
来获得乐趣:您还可以使其递归,可以折叠为 3 行。
You can code
atoi()
for fun:You can also make it recursive, which can fold in 3 lines.
正如已经提到的,
atoi
系列函数不应该在任何 C 程序中使用,因为它们没有任何错误处理。strtol
系列函数 100% 等效,但具有扩展功能:它具有错误处理功能,并且还支持十进制以外的其他基数,例如十六进制或二进制。因此,正确的答案是:使用strtol
(家族)。如果您出于某种原因坚持自己手动推出此功能,则应尝试执行类似于
strtol
的操作,以防除了可选符号和数字之外还存在其他符号。例如,我们想要转换属于较大字符串一部分的数字是很常见的。具有错误处理支持的原始版本可能如下例所示。此代码仅适用于以 10 为基数的十进制数字,但其他方面的行为类似于
strtol
,其中一个可选指针设置为指向遇到的第一个无效符号(如果有)。另请注意,此代码不处理溢出。为了处理溢出,可以添加计算字符的代码,并检查它们是否不会超过 10,假设长度为 32 位
长
,最大长度为 10 位2147483647
。As already mentioned, the
atoi
family of functions should never be used in any C program, since they don't have any error handling.The the
strtol
family of functions is 100% equivalent, but with extended functionality: it has error handling and it also supports other bases than decimal, such as hex or binary. Therefore the correct answer is: usestrtol
(family).If you for some reason insist on rolling out this function yourself manually, you should try to do something similar to
strtol
in case there are other symbols present other than the optional sign and digits. It's quite common that we want to convert numbers that are part of larger string, for example.A naive version with error handling support might look like the example below. This code is for decimal base 10 numbers only, but otherwise behaves like
strtol
with an optional pointer set to point at the first invalid symbol encountered (if any). Also note that this code doesn't handle overflows.To handle overflows, one can for example add code counting the characters and check that they never go past 10, assuming 32 bit
long
which can be max2147483647
, 10 digits.只是想分享一个 unsigned long 的解决方案。
Just wanted to share a solution for unsigned long aswell.
好吧,我遇到了同样的问题。我想出了这个解决方案。它对我来说是最好的。我确实尝试了 atoi() 但对我来说效果不佳。所以这是我的解决方案:
Ok, I had the same problem.I came up with this solution.It worked for me the best.I did try atoi() but didn't work well for me.So here is my solution:
要从字符串中解析整数,我在 C99 中使用此函数:
它返回数字(正数或负数),如果出现错误则返回零。
To parse an integer from a string i'm using this function in C99 :
It return the number (positive or negative), zero in case of error.
您随时可以自己推出!
这将做你想做的事情而不混乱。
You can always roll your own!
This will do what you want without clutter.
此功能将帮助您
参考:http://amscata.blogspot.com/ 2013/09/strnumstr-version-2.html
This function will help you
Ref: http://amscata.blogspot.com/2013/09/strnumstr-version-2.html
在C++中,您可以使用这样的函数:
这可以帮助您将任何字符串转换为任何类型,例如float、int、double...
In C++, you can use a such function:
This can help you to convert any string to any type such as float, int, double...
是的,您可以直接存储整数:
如果您必须解析字符串,
atoi
或strol
将赢得“最短代码量”竞赛。Yes, you can store the integer directly:
If you must parse a string,
atoi
orstrol
is going to win the "shortest amount of code" contest.