4

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* 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];

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.

Community
  • 1
  • 1
Aicos
  • 182
  • 1
  • 14
  • To request support for reading unsigned values from NSString, please visit http://bugreport.apple.com and file a dupe of radar://2264733 against component `Foundation | X`. – Quinn Taylor Jan 23 '13 at 05:48

1 Answers1

2

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.

Chris Gregg
  • 2,376
  • 16
  • 30
  • Yes, seems like the formatter rounded it up in some way. Thanks for looking into it anyway. – Aicos Jul 09 '11 at 14:21