C语言 无符号整数的 atoi 等价物
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27260304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Equivalent of atoi for unsigned integers
提问by John
I'm doing two operations involving atoi and I'm wondering how I can do this with unsigned integers because atoi seems to convert these to signed causing a wraparound integer overflow. I want to work with 32bit unsigned integers but atoi is limiting me effectively to 31bit unsigned.
我正在做两个涉及 atoi 的操作,我想知道如何使用无符号整数执行此操作,因为 atoi 似乎将这些转换为有符号整数,从而导致环绕整数溢出。我想使用 32 位无符号整数,但 atoi 将我有效地限制为 31 位无符号整数。
if (multiplication_is_safe(atoi(argv[1]),atoi(argv[3])))
{
printf("%s * %s = %u \n", argv[1], argv[3], atoi(argv[1]) * atoi(argv[3]));
return 0;
} else
回答by This isn't my real name
The simple answer is to use strtoul()instead.
简单的答案是strtoul()改用。
The longer answer is that even if all you needed was signed 32 bit integers or were happy with 31 bits for unsigned, the atoi()function is a poor fit for what you appear to be doing.
更长的答案是,即使您需要的只是有符号的 32 位整数,或者对 31 位的无符号整数感到满意,该atoi()函数也不适合您似乎正在做的事情。
As you have already noted, the atoi()function converts a string to an integer. A normal, signed integer. However, what atoi()doesn'tdo is error handling. What atoi()'s specification says is "If the value cannot be represented, the behavior is undefined."
正如您已经注意到的,该atoi()函数将字符串转换为整数。一个正常的有符号整数。但是,atoi()没有做的是错误处理。什么atoi()的规范说的是‘如果该值不能表示,该行为是不确定的。’
The strto*() family of functions all clearly specify how errors are handled, so you should in all cases replace atoi()with calls to strtol()(convert string to long), and in this case since you want to handle unsigned integers, you should use strtoul()(convert string to unsigned long).
strto*() 系列函数都清楚地指定了错误的处理方式,因此在所有情况下都应替换atoi()为对strtol()(convert string to long) 的调用,在这种情况下,由于要处理无符号整数,应使用strtoul()(convert字符串到无符号长)。
Also note that if you want to handle larger numbers, there are the strtoll()and strtoull()functions, to convert your string to a long long or an unsigned long long. (And if you just want to handle the largest possible integral values without bothering with all that stuff in between, there's strtoimax()and strtoumax(), that return values of type intmax_tor uintmax_trespectively.)
另请注意,如果您想处理更大的数字,可以使用strtoll()和strtoull()函数将字符串转换为 long long 或 unsigned long long。(如果您只想处理最大可能的整数值,而不想处理中间的所有内容,则有strtoimax()and strtoumax(),分别返回类型为intmax_tor 的值uintmax_t。)
POSIX Documentation:
POSIX 文档:
回答by The Archetypal Paul
Depending on your platform, strtoulis probably what you want:
根据您的平台,strtoul可能是您想要的:
The strtoul() function converts the initial part of the string in nptr to an unsigned long int value according to the given base, which must be between 2 and 36 inclusive, or be the special value 0.
strtoul() 函数根据给定的基数将 nptr 中字符串的初始部分转换为 unsigned long int 值,该值必须介于 2 和 36 之间(包括 2 和 36),或者是特殊值 0。

