Convert NSString to Unsigned long

I have an NSString value that I want to convert to unsigned long . I used the code below

 NSString *updateId = [NSString stringWithFormat:@"%@",[tweet updateId]]; NSLog(@"%@",[tweet updateId]); unsigned long tweetId = [updateId longLongValue]; NSLog(@"ButtonPressed: .......%llu",tweetId); 

But it does not return the correct value ...

+6
source share
3 answers

Cocoa will use [NSNumberFormater] 1 :

 NSNumberFormatter* formatter = [[NSNumberFormatter alloc] init]; NSLog(@"%lu", [[formatter numberFromString:updateId] unsignedLongValue]); [formatter release]; 

What type of [tweet updateId]?
Does it contain any localization (for example, thousands of delimiters)?
If so, you can configure the NSNumberFormatter instance with setLocale:

+11
source

You can try the atol function from C stdlib:

 unsigned long tweetId = atol( [ updateId cStringUsingEncoding: NSASCIIStringEncoding ] ); 

And by the way, your NSLog should be lu , not llu .

0
source
 unsigned long tweetId = [[NSNumber numberWithInteger:[updateId integerValue]] unsignedLongValue]; 
0
source

Source: https://habr.com/ru/post/887172/


All Articles