Created
May 6, 2020 16:39
-
-
Save KyNorthstar/ce1e62fe0194ca969eb7cdda6639a011 to your computer and use it in GitHub Desktop.
Swiftier version of https://stackoverflow.com/a/42079976/3939277
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 | |
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