Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSString to UInt64

How do you convert from NSString to UInt64?

For example, something like this if a UInt64Value helper method existed:

NSString *value = @"1234567";
UInt64 convertedValue = [value UInt64Value];

I am trying to do this in an iOS project.

like image 672
Alexandru Avatar asked Dec 11 '22 17:12

Alexandru


2 Answers

To pitch another possibility for completeness:

NSScanner *scanner = [NSScanner scannerWithString:value];
unsigned long long convertedValue = 0;
[scanner scanUnsignedLongLong:&convertedValue];
return convertedValue;

... check the result on scanUnsignedLongLong if you want to differentiate between finding a value of 0 and finding something that isn't a number.

like image 138
Tommy Avatar answered Dec 26 '22 21:12

Tommy


NSString * num = @"123456";

NSNumberFormatter * numberFormatter = [[NSNumberFormatter alloc]init];
NSNumber *  number = [numberFormatter numberFromString:num];
unsigned long long valueUInt64 = number.unsignedLongLongValue;
like image 20
Muhammad Zohaib Ehsan Avatar answered Dec 26 '22 23:12

Muhammad Zohaib Ehsan