可能重复:
Storing and retrieving unsigned long long value to/from NSString
尝试将NSString转换为unsigned long long时遇到问题。下面是代码:
NSString* mp3id = [[NSString alloc] initWithUTF8String:"16902439379132728577"];
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
NSNumber* persistentId = [f numberFromString:mp3id];
unsigned long long test = [persistentId unsignedLongLongValue];
NSLog(@"unsigned long long is %llu",test);
[f release];
输出:
2011-07-09 15:28:06.834 NBUnityMP3Plugin [443:307] unsigned long long is 16902439379132729344 2011-07-09 15:28:06.847 NBUnityMP3Plugin [443:307]持久性id为1.690243937913273e + 19
要清楚明白:
16902439379132728577 - input
16902439379132729344 - output
理想情况下,我希望将无符号long long转换为NSNumber变量。我认为如果我只有一个无符号长的长就很容易。
答案 0 :(得分:2)
NSNumberFormatter将unsigned long long表示为float,我认为在初始化[f setAllowsFloats:NO];
之后设置NSNumberFormatter
可以解决问题。但是,它似乎没有这样做(在尝试设置数字时返回nil
)。对于NSNumberFormatter来说,16902439379132728577
可能太长而无法作为整数处理,并且它可能只处理signed
范围内的long long
个整数。