4
let temp: String = "0xffeeffff"

How to convert above String to UInt32, because I need to store it in the bitmap which only accept UInt32

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
shixiaoz
  • 73
  • 1
  • 7

2 Answers2

4

Remove "0x" from your string to convert it to UInt32:

let temp   = "0xffeeffff"
let result = UInt32(String(temp.characters.dropFirst(2)), radix: 16)
Devran Cosmo Uenal
  • 6,055
  • 2
  • 26
  • 29
2

hope this is help you...

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
        }
    }
}
}
Akash Raghani
  • 557
  • 1
  • 9
  • 21