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

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

问题描述

我想知道如何在 Objective-C 中解析表示数字的十六进制字符串.我愿意同时使用目标方法,或者基于 C 的方法,两者都可以.

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.

示例:

#01FFFFAB

应该解析成整数:33554347

should parse into the integer: 33554347

任何帮助将不胜感激!

推荐答案

Joshua Weinberg 的回答大部分是正确的,但是在扫描十六进制整数时 0x 前缀是可选的.如果你有一个 #01FFFFAB 格式的字符串,你仍然可以使用 NSScanner,但你可以跳过第一个字符.

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天全站免登陆