Abhishek
Abhishek

Reputation: 303

Convert NSString to Unsigned long

I am having NSString value which I want to convert in unsigned long. I used code below

NSString *updateId = [NSString stringWithFormat:@"%@",[tweet updateId]];

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

But it is not returning correct value...

Upvotes: 6

Views: 6819

Answers (3)

Julian F. Weinert
Julian F. Weinert

Reputation: 7570

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

Upvotes: 0

Macmade
Macmade

Reputation: 54059

You may try the atol function from the C stdlib:

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

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

Upvotes: 0

Thomas Zoechling
Thomas Zoechling

Reputation: 34263

The Cocoa way would be to use [NSNumberFormater]1:

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

What's the the type of [tweet updateId]?
Does it contain any localization (e.g. thousand separators)?
If so, you could configure the NSNumberFormatter instance with setLocale:

Upvotes: 11

Related Questions