将十六进制字符串转换为长字符串

发布于 2024-08-14 06:40:47 字数 178 浏览 6 评论 0原文

是否有任何 Cocoa 类可以帮助我将 NSString 中的十六进制值(如 0x12FA)转换为 longNSNumber?看起来像 NSNumberFormatter 这样的类都不支持十六进制数字。

谢谢, 华英

Are there any Cocoa classes that will help me convert a hex value in a NSString like 0x12FA to a long or NSNumber? It doesn't look like any of the classes like NSNumberFormatter support hex numbers.

Thanks,
Hua-Ying

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

拥醉 2024-08-21 06:40:47

以下是如何使用 NSScanner 执行此操作的简短示例:

NSString* pString = @"0xDEADBABE";
NSScanner* pScanner = [NSScanner scannerWithString: pString];

unsigned int iValue;
[pScanner scanHexInt: &iValue];

Here's a short example of how you would do it using NSScanner:

NSString* pString = @"0xDEADBABE";
NSScanner* pScanner = [NSScanner scannerWithString: pString];

unsigned int iValue;
[pScanner scanHexInt: &iValue];
甜妞爱困 2024-08-21 06:40:47

请参阅NSScanner 的 scanHex...:方法。这将为您提供可以包装在 NSNumber 中的原语。

See NSScanner's scanHex...: methods. That'll get you the primitive that you can wrap in an NSNumber.

℉絮湮 2024-08-21 06:40:47

这是另一种转换方式,将 long long int 转换为十六进制字符串。
首先将十六进制改为long long。

NSString* pString = @"ffffb382ddfe";
NSScanner* pScanner = [NSScanner scannerWithString: pString];

unsigned long long iValue2;
[pScanner scanHexLongLong: &iValue2];

NSLog(@"iValue2 = %lld", iValue2);

另一种方式,longlong 到十六进制字符串......

NSNumber *number;
NSString *hexString;

number = [NSNumber numberWithLongLong:iValue2];
hexString = [NSString stringWithFormat:@"%qx", [number longLongValue]];

NSLog(@"hexString = %@", hexString);

here is the other way conversion, a long long int to hex string.
first the hex to long long.

NSString* pString = @"ffffb382ddfe";
NSScanner* pScanner = [NSScanner scannerWithString: pString];

unsigned long long iValue2;
[pScanner scanHexLongLong: &iValue2];

NSLog(@"iValue2 = %lld", iValue2);

and the other way, longlong to hex string...

NSNumber *number;
NSString *hexString;

number = [NSNumber numberWithLongLong:iValue2];
hexString = [NSString stringWithFormat:@"%qx", [number longLongValue]];

NSLog(@"hexString = %@", hexString);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文