Objective-C将十六进制字符串解析为整数 [英] Objective-C parse hex string to integer

查看:122
本文介绍了Objective-C将十六进制字符串解析为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在Objective-C中解析表示数字的十六进制字符串。例如:


pre> #01FFFFAB

应解析为整数:
33554347



任何帮助将不胜感激!

约书亚Weinberg的回答大部分都是正确的,但是当扫描十六进制整数时, 0x 前缀是可选的。如果您的格式为#01FFFFAB ,您仍然可以使用 NSScanner ,但您可以跳过第一个字符。

 无符号结果= 0; 
NSScanner * scanner = [NSScanner scannerWithString:@#01FFFFAB];

[scanner setScanLocation:1]; //绕过'#'字符
[scanner scanHexInt:& result];


I would like to know how to parse a hex string, representing a number, in Objective-C. I am willing to use both an objective, or a C-based method, either is fine.

example:

#01FFFFAB

should parse into the integer: 33554347

Any help would be appreciated!

解决方案

Joshua Weinberg's answer is mostly correct, however the 0x prefix is optional when scanning hexadecimal integers. If you have a string in the format #01FFFFAB, you can still use NSScanner, but you can skip the first character.

unsigned result = 0;
NSScanner *scanner = [NSScanner scannerWithString:@"#01FFFFAB"];

[scanner setScanLocation:1]; // bypass '#' character
[scanner scanHexInt:&result];

这篇关于Objective-C将十六进制字符串解析为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆