Skip to content

Instantly share code, notes, and snippets.

@danieleggert
Created August 30, 2015 12:09
Show Gist options
  • Save danieleggert/a2840bb77fd183f6470b to your computer and use it in GitHub Desktop.
Save danieleggert/a2840bb77fd183f6470b to your computer and use it in GitHub Desktop.
Hexadecimal string from NSDate
import Foundation
extension NSData {
var hexadecimalString: String {
var result = ""
enumerateByteRangesUsingBlock { (pointer, range, _) in
let pointerToBytes = UnsafePointer<UInt8>(pointer)
let bytes = UnsafeBufferPointer(start: pointerToBytes, count: range.length)
for v in bytes {
result.appendContentsOf(String(format:"%02x", v))
}
}
return result
}
}
let d = NSData(bytes: [0xDE, 0xAD, 0xBE, 0xEF] as [UInt8], length: 4)
let c = d.hexadecimalString // deadbeef
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment