开发者

Convert NSString to Unsigned long

开发者 https://www.devze.com 2023-03-02 16:23 出处:网络
I am having NSString value which I want to convert in unsigned long. I used code below NSString *updateId = [NSString stringWithFormat:@\"%@\",[tweet updat开发者_运维技巧eId]];

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

精彩评论

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