开发者

Parsing 64-bit unsigned integer on iOS

开发者 https://www.devze.com 2023-01-20 12:56 出处:网络
I have very big unsigned integral number in NSString. This may be big to 2^64. Is there an existing functions/classes parsing this?

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 开发者_Go百科use to parse this.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消