Problems converting NSString to unsigned long long [duplicate]
Possible Duplicate:
Storing and retrieving unsigned long long value to/from NSString
Having problems trying to convert an NSString to an unsigned long long. Heres the code:
NSString* mp3开发者_如何学编程id = [[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];
Output:
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] persistant id is 1.690243937913273e+19
To make it clearner:
16902439379132728577 - input
16902439379132729344 - output
Ideally I would like to get that unsigned long long into an NSNumber variable. I figured that would be easy if I only had an unsigned long long.
NSNumberFormatter is representing the unsigned long long as a float, and I thought setting [f setAllowsFloats:NO];
after initializing the NSNumberFormatter
would fix the problem. It doesn't seem to do that, though (returning nil
when trying to set the number). It is possible that 16902439379132728577
is too long for NSNumberFormatter to handle as an integer, and it may only handle signed
integers in the long long
range.
精彩评论