Objective-C 将十六进制字符串解析为整数

发布于 2024-09-17 06:45:17 字数 164 浏览 5 评论 0原文

我想知道如何在 Objective-C 中解析代表数字的十六进制字符串。我愿意使用客观方法或基于 C 的方法,两者都可以。

示例:

#01FFFFAB

应解析为整数: 33554347

任何帮助将不胜感激!

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!

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

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

发布评论

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

评论(7

九厘米的零° 2024-09-24 06:45:17

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

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

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

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];
天荒地未老 2024-09-24 06:45:17

您可以使用 NSScanner 对于这个

unsigned int outVal;
NSScanner* scanner = [NSScanner scannerWithString:@"0x01FFFFAB"];
[scanner scanHexInt:&outVal];

outVal 将包含您要查找的 int。 0x 是可选的。

you can use NSScanner for this

unsigned int outVal;
NSScanner* scanner = [NSScanner scannerWithString:@"0x01FFFFAB"];
[scanner scanHexInt:&outVal];

outVal will contain the int you're looking for. The 0x is optional.

谈情不如逗狗 2024-09-24 06:45:17

strtol() 是你的朋友。

它将字符串转换为 long,并且您可以传入数字的基数。不过,首先要去掉 # 符号,或者将指向第一个数字字符的指针传递给 strtol。

strtol() is your friend.

It converts a string to a long, and you can pass the base of the number in. Strip that # sign off first though, or pass to strtol a pointer to the first numerical character.

末蓝 2024-09-24 06:45:17

您可以使用以下行进行转换。它只有一行代码:

NSString *hexString = @"01FFFFAB";
length = (UInt64)strtoull([hexString UTF8String], NULL, 16);
NSLog(@"The required Length is %d", length);

快乐编码!!!

You can use the below line for conversion. Its just one line code:

NSString *hexString = @"01FFFFAB";
length = (UInt64)strtoull([hexString UTF8String], NULL, 16);
NSLog(@"The required Length is %d", length);

Happy Coding!!!

你好,陌生人 2024-09-24 06:45:17

Swift 4 标准库引入了新的初始化程序来解析所有整数类型。它需要字符串以基数(即基数)进行解析并返回可选整数:

let number = Int("01FFFFAB", radix: 16)!

Swift 4 standard library introduced new initializer for parsing all integer types. It takes string to parse with radix (i.e. base) and returns optional integer:

let number = Int("01FFFFAB", radix: 16)!
二智少女猫性小仙女 2024-09-24 06:45:17

根据苹果公司的说法:

NSScanner 对象解释并转换
NSString 对象转换为数字和字符串值。

所以,如果你有 NSData obj 你可以做下一步

NSString *dataDescription = data.description;
NSString *dataAsString = [dataDescription substringWithRange:NSMakeRange(1, [dataDescription length]-2)];
unsigned intData = 0;
NSScanner *scanner = [NSScanner scannerWithString:dataAsString];
[scanner scanHexInt:&intData];

According to apple:

An NSScanner object interprets and converts the characters of an
NSString object into number and string values.

so, if u have NSData obj u can do next

NSString *dataDescription = data.description;
NSString *dataAsString = [dataDescription substringWithRange:NSMakeRange(1, [dataDescription length]-2)];
unsigned intData = 0;
NSScanner *scanner = [NSScanner scannerWithString:dataAsString];
[scanner scanHexInt:&intData];
猫九 2024-09-24 06:45:17

对于斯威夫特 3:

var hex = "#01FFFFAB"
hex.remove(at: hex.startIndex)
var rgbValue:UInt32 = 0
Scanner(string: hex).scanHexInt32(&rgbValue)
// rgbValue == 33554347

For Swift 3:

var hex = "#01FFFFAB"
hex.remove(at: hex.startIndex)
var rgbValue:UInt32 = 0
Scanner(string: hex).scanHexInt32(&rgbValue)
// rgbValue == 33554347
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文