Storing and retrieving unsigned long long value to/from NSString
Asked Answered
C

1

24

I have an unsigned long long value which I want to store into an NSString and retrieve from the string.

Initially I have the value in an NSNumber and I am using this to get the string

NSString *numStr = [NSString stringWithFormat:@"%llu", [myNum unsignedLongLongValue]];

where myNum is an NSNumber.

To get back the NSNumber from the NSString I have to first get the unsigned long long value. But there is no method in the NSString class to do that (we just have one for getting the long long value, not the unsigned long long value).

Can someone please tell me how I can get back the value into an NSNumber variable.

Thanks.

Crossland answered 25/7, 2009 at 8:55 Comment(0)
E
59

There are a lot of ways to accomplish this. The following is the most pragmatic:

NSString *numStr = [NSString stringWithFormat:@"%llu", [myNum unsignedLongLongValue]];

// .. code and time in between when numStr was created
// .. and now needs to be converted back to a long long.
// .. Therefore, numStr used below does not imply the same numStr above.

unsigned long long ullvalue = strtoull([numStr UTF8String], NULL, 0);

This makes a few reasonable assumptions such as numStr will only contain numeric digits and it contains a 'valid' unsigned long long value. A drawback to this approach is that UTF8String creates what essentially amounts to [[numStr dataUsingEncoding:NSUTF8StringEncoding] bytes], or in other words something along the lines of 32 bytes of autoreleased memory per call. For the vast majority of uses, this is no problem what-so-ever.

For an example of how to add something like unsignedLongLongValue to NSString that is both very fast and uses no autoreleased memory as a side effect, take a look at the end of my (long) answer to this SO question. Specifically the example implementation of rklIntValue, which would require only trivial modifications to implement unsignedLongLongValue.

More information regarding strtoull can be found in its man page.

Eyesore answered 25/7, 2009 at 9:24 Comment(5)
Excellent answer. You could also just read the characters out in to a local temporary C string (since the characters are all digits, there is no UTF-8 or Unicode issues) and then use strtoull which would avoid the described drawback, but it would be a very weird case where your drawback was an actual issue in practice.Negress
there are no unsignedLongLongValue method for NSString objectPoynter
To request support for reading unsigned values from NSString, please visit bugreport.apple.com and file a dupe of radar://2264733 against component Foundation | X.Nevernever
Why aren't you setting the base parameter of strtoull to 10 instead of 0? The documentation says that strings starting with 0 are considered octal values, this could become a hard to find bug once you happen to encounter a string like this e.g. from human input. So my proposal is: unsigned long long ullvalue = strtoull([numStr UTF8String], NULL, 10);Arsenide
@Johne The base should be 10 hereSeem

© 2022 - 2024 — McMap. All rights reserved.