I’m trying to convert an NSString object to an NSNumber with the same numerical value. When I create the NSString object using this statement…
NSString *songID = [localNotif.userInfo objectForKey:@"SongID"];
the var songID contains the string value @"10359537395704663785". and when I attempt to convert it to an NSNumber object using the statement…
NSNumber *songIDNumber = [NSNumber numberWithLongLong:[songID longLongValue]];
the var songIDNumber contains the wrong value of 9223372036854775807
What am I doing wrong? It might also be worth noting that sometimes this code does work and produce the correct NSNumber value, but most of the time it fails as shown above.
Thanks in advance for your help!
UPDATE: God I love this site! Thanks to unbeli and carl, I was able to fix it using the updated code for converting from the NSString to the NSNumber…
unsigned long long ullvalue = strtoull([songID UTF8String], NULL, 0);
NSNumber *songIDNumber = [NSNumber numberWithUnsignedLongLong:ullvalue];
9223372036854775807decimal is0x7FFFFFFFFFFFFFFFin hex. Your number is10359537395704663785, which is too large for along longand overflows. From the NSString documentation: