4

I have very big unsigned integral number in NSString. This may be big to 2^64. Is there an existing functions/classes parsing this?

As I know, it's unsigned long long value, but, It's hard to know what kind of method should I use to parse this.

eonil
  • 75,400
  • 74
  • 294
  • 482
  • what exactly you mean by parsing? the conversion from your NSString number to unsigned long long? – nacho4d Oct 21 '10 at 06:23
  • 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:47

3 Answers3

9

Perhaps not the prettiest answer, but you should be able to do something like this:

#include <stdlib.h>
...
unsigned long long parsedValue = strtoull([yourString UTF8String], NULL, 0);

Someone else might have a more cocoa-ey way of doing it.

Stephen Canon
  • 97,302
  • 18
  • 172
  • 256
1

This is an old one but here's a slightly more Cocoa-ey variant of Stephen Canon's (correct) answer:

#import <stdlib.h>

@implementation NSString (MyHelpers)
    - (unsigned long long)unsignedLongLongValue {
        return strtoull([self UTF8String], NULL, 0);
    }
@end

Now all strings will respond to the unsignedLongLongValue selector.

Archaeopterasa
  • 478
  • 6
  • 8
0

This might work, though it only gets to 2^63 - 1:

NSScanner *scanner = [NSScanner scannerWithString:numericString];
long long valueFromString;
if(![scanner scanLongLong:&valueFromString]) {
    //ERROR
}

There doesn't appear to be a scanUnsignedLongLong method for NSScanner.

Jay Peyer
  • 1,999
  • 1
  • 18
  • 17
  • Thanks for answer, but that's just the trouble what I'd explained. The problem is just the value can be up to 2^64. – eonil Oct 23 '10 at 07:25