开发者

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 updat开发者_运维技巧eId]];

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

But it is not returning correct value...


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:


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜