5

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Alexandru
  • 12,264
  • 17
  • 113
  • 208

4 Answers4

10

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.

Tommy
  • 99,986
  • 12
  • 185
  • 204
5
NSString * num = @"123456";

NSNumberFormatter * numberFormatter = [[NSNumberFormatter alloc]init];
NSNumber *  number = [numberFormatter numberFromString:num];
unsigned long long valueUInt64 = number.unsignedLongLongValue;
  • Why do you use this over [stringValue longLongValue]; ? – Alexandru Mar 24 '16 at 17:33
  • because you are asking for unsigned long long :) – Muhammad Zohaib Ehsan Mar 24 '16 at 17:34
  • I am not familiar with this datatype, long long...perhaps an explanation of it would help? It sounds really strange because I've never encountered such a thing. The 64 bit value space allows at maximum a UInt64, so what is a long long? – Alexandru Mar 24 '16 at 17:34
  • Also another question, what happens if you run this on a 32 bit device? Does it still work? – Alexandru Mar 24 '16 at 17:37
  • I guess @Alexandru you have to search it on your own.I have just nsnumberformatter and found it would be a help for you :). you can have look at this http://superuser.com/questions/698312/if-32-bit-machines-can-only-handle-numbers-up-to-232-why-can-i-write-100000000 – Muhammad Zohaib Ehsan Mar 24 '16 at 17:40
  • 1
    I guess it depends upon the number.if the number is huge and cannot be accommodated in 32 bit.Then it will be stored on 2 32-bit portions. Then it will work.You can have further search also :) – Muhammad Zohaib Ehsan Mar 24 '16 at 17:43
  • 1
    In C, a `long` is 32 bits (on most platforms). To ensure that one is getting a 64 bit value, the `long long` type exists. To get the unsigned version, one simply prefaces the type with `unsigned`, as is done for any integer type. – Avi Mar 24 '16 at 17:51
  • 3
    Unfortunately this answer is incorrect, try it with `@"18446744073709551615"` (that is the maximum `unsigned long long` value). It is not immediately clear whether this is a bug or feature of `NSNumberFormatter`. The answer by @Tommy works correctly in this scenario. – CRD Mar 24 '16 at 20:11
  • Thanks for the help, guys. Yeah, makes a bit more sense now. I realize coming from the Windows world things are a bit different in the Linux landscape. P.S. you can set `valueUInt64` as a `UInt64` directly: `UInt64 valueUInt64 = number.unsignedLongLongValue;` I guess it makes sense why they call it a `long long` now, as it may have to be stored into two 32-bit `long`'s to ensure it is actually 64 bits long, in a 32-bit architecture. – Alexandru Mar 24 '16 at 23:22
  • @CRD good spot.I verified it and it gives zero value when tested with max value of unsigned long long. – Muhammad Zohaib Ehsan Mar 25 '16 at 06:11
  • 1
    Then we gotta give this one to Tommy. – Alexandru Mar 29 '16 at 18:24
  • 1
    Yep confirmed this does not work for @"18446744073709551615". @Tommy's answer works correctly – Binh Le Oct 12 '20 at 07:40
0

You can use strtoull(const char *, char **, int) as following:

NSString *value = @"1234567";
UInt64 convertedValue = strtoull([value UTF8String], NULL, 0);
Tom Zhang
  • 1
  • 1
  • 1
-1

You can get a UInt64 with longLongValue

NSString *str = @"23147653105732";
UInt64 uint64 = [str longLongValue];

Another way to get an int64 is:

NSString *str = @"23147653105732";
int64_t int64_t = atoll([str UTF8String]);
Haroldo Gondim
  • 7,725
  • 9
  • 43
  • 62