简体   繁体   English

将 NSString 转换为 Unsigned long

[英]Convert NSString to Unsigned long

I am having NSString value which I want to convert in unsigned long .我有NSString值,我想将其转换为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...但它没有返回正确的值......

The Cocoa way would be to use [NSNumberFormater] 1 : Cocoa 方法是使用[NSNumberFormater] 1

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

What's the the type of [tweet updateId]? [tweet updateId] 的类型是什么?
Does it contain any localization (eg thousand separators)?它是否包含任何本地化(例如千位分隔符)?
If so, you could configure the NSNumberFormatter instance with setLocale:如果是这样,您可以使用setLocale:配置NSNumberFormatter实例:

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

You may try the atol function from the C stdlib:您可以尝试 C 标准库中的atol function:

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

And by the way, your NSLog should be lu , not llu .顺便说一句,您的 NSLog 应该是lu ,而不是llu

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM