iOS - int (unsigned int) 最大值是 2147483647,unsigned int 不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14094686/
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
iOS - int (unsigned int) maximum value is 2147483647, unsigned int doesn't work?
提问by Jonathan Gurebo
the maximum value for an 32-Bit integer is: 2^31-1 = 2147483647. but just with negative and positive number. because the half of the number is negative. so the real maximum value is 2^32-1 = 4294967295. but in this case we just use positive numbers.
32 位整数的最大值为:2^31-1 = 2147483647。但只有负数和正数。因为数字的一半是负数。所以真正的最大值是 2^32-1 = 4294967295。但在这种情况下,我们只使用正数。
ok, a normal int is both negative and positive number. i want to use just positive number because i want the maximum value to be: 4294967295. i'm going to use "unsigned int" instead of "int"
好的,正常的 int 是负数和正数。我只想使用正数,因为我希望最大值为:4294967295。我将使用“unsigned int”而不是“int”
but this will not work! the maximum value is still 2147483647.
但这行不通!最大值仍然是 2147483647。
here is the code for a simple random number generator:
这是一个简单的随机数生成器的代码:
-(Action for my button) {
unsigned int minNumber;
unsigned int maxNumber;
unsigned int ranNumber;
minNumber=[self.textFieldFrom.text intValue]; //getting numbers from my textfields
maxNumber=[self.textFieldTo.text intValue]; //Should i use unsigned intValue?
ranNumber=rand()%(maxNumber-minNumber+1)+minNumber;
NSString *str = [NSString stringWithFormat:@"%d", ranNumber];
self.label.text = str;
}
and this will view : 2147483647 as a maximum value.
这将查看: 2147483647 作为最大值。
what's wrong? should i use unsigned intValue when i getting numbers from my textFields?
怎么了?当我从文本字段中获取数字时,我应该使用 unsigned intValue 吗?
Jonathan
乔纳森
here you can read about this number. : http://en.wikipedia.org/wiki/2147483647
您可以在此处阅读有关此号码的信息。: http://en.wikipedia.org/wiki/2147483647
回答by Rob
The problem is intValue
which returns a int
(not an unsigned int
) and is maxed out at 2,147,483,647. If you need larger than INT32_MAX
, then use long
variables and NSString
method longLongValue
instead of intValue
.
问题是intValue
哪个返回 a int
(不是 an unsigned int
)并且最大值为 2,147,483,647。如果您需要大于INT32_MAX
,则使用long
变量和NSString
方法longLongValue
而不是intValue
。
Yes, %d
is signed, but if you use %u
that will not solve the problem with intValue
. Using long long
variables and longlongValue
method will solve this. For example:
是的,%d
已签名,但如果您使用%u
它并不能解决intValue
. 使用long long
变量和longlongValue
方法将解决这个问题。例如:
NSString *string = @"2147483650";
unsigned int i = [string intValue];
NSLog(@"i = %u", i); // returns 2147483647
NSUInteger j = [string integerValue];
NSLog(@"j = %u", j); // returns 2147483647
long long k = [string longLongValue];
NSLog(@"k = %lld", k); // returns 2147483650
So, looking at your original code, it might be:
因此,查看您的原始代码,它可能是:
long long minNumber;
long long maxNumber;
long long ranNumber;
minNumber = [self.textFieldFrom.text longLongValue];
maxNumber = [self.textFieldTo.text longLongValue];
ranNumber = arc4random_uniform(maxNumber-minNumber+1) + minNumber;
NSString *str = [NSString stringWithFormat:@"%lld", ranNumber];
self.label.text = str;
回答by Nickolay Olshevsky
%d modifier is parsed as signed integer.
%d 修饰符被解析为有符号整数。