Last active
July 14, 2018 12:44
-
-
Save kristopherjohnson/ed2623e1b486a8262b12 to your computer and use it in GitHub Desktop.
Convert NSData bytes to hexadecimal string
This file contains 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 { | |
/// Return hexadecimal string representation of NSData bytes | |
@objc(kdj_hexadecimalString) | |
public var hexadecimalString: NSString { | |
var bytes = [UInt8](count: length, repeatedValue: 0) | |
getBytes(&bytes, length: length) | |
let hexString = NSMutableString() | |
for byte in bytes { | |
hexString.appendFormat("%02x", UInt(byte)) | |
} | |
return NSString(string: hexString) | |
} | |
} |
@ast UnsafePointer does not conform to SequenceType
Or simply with one line return self.map{ String(format: "%02x", $0) }.joined()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's not necessary to copy the bytes.