Skip to content

Instantly share code, notes, and snippets.

@nickplee
Created November 10, 2016 18:24
Show Gist options
  • Save nickplee/b3e4936606c0ed091743597204803046 to your computer and use it in GitHub Desktop.
Save nickplee/b3e4936606c0ed091743597204803046 to your computer and use it in GitHub Desktop.
Converts Data objects to hex strings
extension Data {
var hexString: String {
return withUnsafeBytes { (b: UnsafePointer<UInt8>) -> String in
var output = ""
let end = b.advanced(by: count)
var ptr = b
while ptr < end {
let value = ptr[0]
output += String(format: "%02X", arguments: [value])
ptr = ptr.advanced(by: 1)
}
return output
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment