如何将十六进制字符串转换为十进制?
问题描述:
如果您能告诉我如何在 NSString
中转换十六进制字母,例如使用Objective-C将 @50A6C2
改为小数。提前致谢。
I would appreciate it if you could tell me how I can convert hexadecimal letters within an NSString
, e.g. @"50A6C2"
, to decimals using Objective-C. Thanks in advance.
答
最简单的方法是使用 NSScanner ,特别是方法 scanHexInt:
或 scanHexLongLong:
。另一种可能是从NSString获取C字符串,并使用C风格的函数,如 strtol
(以16为基数)。
The easiest way is to use an NSScanner, specifically the methods scanHexInt:
or scanHexLongLong:
. Another possibility is to get the C string from the NSString and use C-style functions such as strtol
(with base 16).