Created
May 23, 2015 17:29
-
-
Save john-07/8b3a5d1c8cef9917997d to your computer and use it in GitHub Desktop.
NSData hexString
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
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