Skip to content

Instantly share code, notes, and snippets.

@john-07
Created May 23, 2015 17:29
Show Gist options
  • Save john-07/8b3a5d1c8cef9917997d to your computer and use it in GitHub Desktop.
Save john-07/8b3a5d1c8cef9917997d to your computer and use it in GitHub Desktop.
NSData hexString
func hexString(data:NSData)->String{
let bytesCount = data.length;
if bytesCount > 0 {
let hexChars = Array("0123456789abcdef".utf8) as [UInt8];
let buf = UnsafeBufferPointer<UInt8>(start: UnsafePointer(data.bytes), count: data.length);
var output = [UInt8](count: data.length*2 + 1, repeatedValue: 0);
var ix:Int = 0;
for b in buf {
let hi = Int((b & 0xf0) >> 4);
let low = Int(b & 0x0f);
output[ix++] = hexChars[ hi];
output[ix++] = hexChars[low];
}
let result = String.fromCString(UnsafePointer(output))!;
return result;
}
return "";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment