Created
August 30, 2015 12:09
-
-
Save danieleggert/a2840bb77fd183f6470b to your computer and use it in GitHub Desktop.
Hexadecimal string from NSDate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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