Search code examples
stringswifthexnsdataswift2

NSData from hex String?


I know this question has been asked for ObjectiveC, but my Swift isn't strong enough yet to transliterate the char *bytes stuff.

So given

let string = "600DBEEF"

How do I create an NSData which represents those 4 bytes: 60 0D BE EF?


Solution

  • With the arrival of Swift3 and the new Foundation Data type, I finally circled back to this:

    extension UnicodeScalar {
        var hexNibble:UInt8 {
            let value = self.value
            if 48 <= value && value <= 57 {
                return UInt8(value - 48)
            }
            else if 65 <= value && value <= 70 {
                return UInt8(value - 55)
            }
            else if 97 <= value && value <= 102 {
                return UInt8(value - 87)
            }
            fatalError("\(self) not a legal hex nibble")
        }
    }
    
    extension Data {
        init(hex:String) {
            let scalars = hex.unicodeScalars
            var bytes = Array<UInt8>(repeating: 0, count: (scalars.count + 1) >> 1)
            for (index, scalar) in scalars.enumerated() {
                var nibble = scalar.hexNibble
                if index & 1 == 0 {
                    nibble <<= 4
                }
                bytes[index >> 1] |= nibble
            }
            self = Data(bytes: bytes)
        }
    }
    

    Now I can construct Data objects in a fashion similar to their printed form:

    Data(hex: "600dBeef")