字符串转换为大整数值
String conversion to large integer value
我正在使用下面的代码。我可以成功获取字符串值。但是当转换成NSInteger
的时候,前面出现了一个负号,值就变了。我错过了什么吗?
NSInteger bannerStamp = [[eachDict objectForKey:@"timeStamp"] integerValue];
NSLog(@"%@",[eachDict objectForKey:@"timeStamp"]);
NSLog(@"%d",bannerStamp);
输出
2015-01-01 10:44:52.482 SalesApp[24570:60b] 3597478187
2015-01-01 10:44:54.094 SalesApp[24570:60b] -697489109
这就是我从头到尾得到的结果。检查此代码并输出各种方法。
//*******
NSLog(@"For SO question");
NSDictionary *eachDict = @{ @"timeStamp" : @"3597478187" };
NSDecimalNumber *decimalBannerStamp = [NSDecimalNumber decimalNumberWithString:[eachDict objectForKey:@"timeStamp"]];
NSInteger bannerStamp = [[eachDict objectForKey:@"timeStamp"] integerValue];
uint64_t bannerStampUINT64 = [[eachDict objectForKey:@"timeStamp"] longLongValue];
NSLog(@"%@",[eachDict objectForKey:@"timeStamp"]);
NSLog(@"NSInteger: %u",bannerStamp);
NSLog(@"uint64_t: %llu",bannerStampUINT64);
NSLog(@"DecimalObject: %@", decimalBannerStamp);
NSLog(@"Decimal unsigned value: %lu", (unsigned long)decimalBannerStamp.unsignedIntegerValue);
NSLog(@"End SO question code");
//*******
2014-12-31 22:14:50.206 PIClient[5112:2296154] For SO question
2014-12-31 22:14:50.212 PIClient[5112:2296154] 3597478187
2014-12-31 22:14:50.213 PIClient[5112:2296154] NSInteger: 2147483647
2014-12-31 22:14:50.213 PIClient[5112:2296154] uint64_t: 3597478187
2014-12-31 22:14:50.214 PIClient[5112:2296154] DecimalObject: 3597478187
2014-12-31 22:14:50.215 PIClient[5112:2296154] Decimal unsigned value: 3597478187
2014-12-31 22:14:50.215 PIClient[5112:2296154] End SO question code
尝试转换成long long值
long long bannerStamp = [[eachDict objectForKey:@"timeStamp"] longLongValue];
NSLog(@"%@",[eachDict objectForKey:@"timeStamp"]);
NSLog(@"%lld",bannerStamp);
下面是32位和64位架构的区别,
#if __LP64__ || (TARGET_OS_EMBEDDED && !TARGET_OS_IPHONE) || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64
typedef long NSInteger;
typedef unsigned long NSUInteger;
#else
typedef int NSInteger;
typedef unsigned int NSUInteger;
#endif
Apple 文档 link changes to data types