Created
May 31, 2017 21:37
-
-
Save kconner/48f3801cd9f2677b04f1154a234cbb39 to your computer and use it in GitHub Desktop.
Print the bytes of a Data in hexadecimal
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
private func printData(_ data: Data) { | |
let hexBytes = data.map { byte in | |
String(format: "%02x", byte) | |
} | |
for (offset, hexByte) in hexBytes.enumerated() { | |
let terminator: String | |
switch offset % 8 { | |
case 7: | |
terminator = "\n" | |
case 3: | |
terminator = " " | |
default: | |
terminator = " " | |
} | |
print(hexByte, terminator: terminator) | |
} | |
print() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment