let temp: String = "0xffeeffff"
如何将上面的字符串转换为 UInt32,因为我需要将它存储在只接受 UInt32 的位图中
从字符串中删除“0x”以将其转换为 UInt32:
let temp = "0xffeeffff"
let result = UInt32(String(temp.characters.dropFirst(2)), radix: 16)
希望这对你有帮助...
extension String {
func toUInt() -> UInt? {
if contains(self, "-") {
return nil
}
return self.withCString { cptr -> UInt? in
var endPtr : UnsafeMutablePointer<Int8> = nil
errno = 0
let result = strtoul(cptr, &endPtr, 10)
if errno != 0 || endPtr.memory != 0 {
return nil
} else {
return result
}
}
}
}