Skip to content

Instantly share code, notes, and snippets.

@KyLeggiero
Created May 6, 2020 16:39
Show Gist options
  • Save KyLeggiero/ce1e62fe0194ca969eb7cdda6639a011 to your computer and use it in GitHub Desktop.
Save KyLeggiero/ce1e62fe0194ca969eb7cdda6639a011 to your computer and use it in GitHub Desktop.
import Foundation
public extension Array where Element == UInt8 {
// MARK: - Constants
/// This is used by the `toHexString()` method
private static let uppercaseHexCharacters : [Character] =
[ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" ]
// MARK: - Public methods
/// Converts a byte array into a string containing uppercase hex characters, without any additional formatting.
func toHexString() -> String {
var stringToReturn = ""
for byte in self {
let intValue = Int(byte)
stringToReturn.append(Self.uppercaseHexCharacters[intValue >> 4])
stringToReturn.append(Self.uppercaseHexCharacters[intValue & 0x0f])
}
return stringToReturn
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment