4
let temp: String = "0xffeeffff"

如何将上面的字符串转换为 UInt32,因为我需要将它存储在只接受 UInt32 的位图中

4

2 回答 2

4

从字符串中删除“0x”以将其转换为 UInt32:

let temp   = "0xffeeffff"
let result = UInt32(String(temp.characters.dropFirst(2)), radix: 16)
于 2016-01-04T01:20:08.597 回答
2

希望这对你有帮助...

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
        }
    }
}
}
于 2016-01-04T09:21:47.190 回答