更新时间:2022-11-18 18:22:40
您可以使用 Foundation 框架中的 NSScanner()
:
You can use NSScanner()
from the Foundation framework:
let scanner = NSScanner(string: str)
var result : UInt32 = 0
if scanner.scanHexInt(&result) {
println(result) // 37331519
}
或者BSD库函数strtoul()
let num = strtoul(str, nil, 16)
println(num) // 37331519
从 Swift 2 (Xcode 7) 开始, 所有整数类型都有一个
As of Swift 2 (Xcode 7), all integer types have an
public init?(_ text: String, radix: Int = default)
初始化器,以便提供纯 Swift 解决方案:
initializer, so that a pure Swift solution is available:
let str = "239A23F"
let num = Int(str, radix: 16)