Skip to content

Instantly share code, notes, and snippets.

@bmitchelmore
Created March 12, 2024 03:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmitchelmore/7bb40e33d1e1a8440e63651283a22596 to your computer and use it in GitHub Desktop.
Save bmitchelmore/7bb40e33d1e1a8440e63651283a22596 to your computer and use it in GitHub Desktop.
Efficient Data to hex string
extension Data {
private static let hexDigits = Array("0123456789abcdef".utf8)
var hex: String {
var chars = [Unicode.UTF8.CodeUnit]()
chars.reserveCapacity(2 * count)
for byte in self {
chars.append(Self.hexDigits[Int(byte >> 4)]) // byte / 16
chars.append(Self.hexDigits[Int(byte & 15)]) // byte % 16
}
return String(decoding: chars, as: UTF8.self)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment