将NSString转换为unsigned long long时出现问题

时间:2011-07-09 13:36:49

标签: objective-c ios

  

可能重复:
  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变量。我认为如果我只有一个无符号长的长就很容易。

1 个答案:

答案 0 :(得分:2)

NSNumberFormatter将unsigned long long表示为float,我认为在初始化[f setAllowsFloats:NO];之后设置NSNumberFormatter可以解决问题。但是,它似乎没有这样做(在尝试设置数字时返回nil)。对于NSNumberFormatter来说,16902439379132728577可能太长而无法作为整数处理,并且它可能只处理signed范围内的long long个整数。