Created
July 27, 2021 06:46
-
-
Save modestman/79d5d320fccd5a6127f1c5d38899fa4c to your computer and use it in GitHub Desktop.
Hex encoded Data swift
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 Data { | |
struct HexEncodingOptions: OptionSet { | |
public let rawValue: Int | |
public static let upperCase = HexEncodingOptions(rawValue: 1 << 0) | |
public init(rawValue: Int) { | |
self.rawValue = rawValue | |
} | |
} | |
/// Represent data as HEX string | |
func hexEncodedString(options: HexEncodingOptions = []) -> String { | |
let format = options.contains(.upperCase) ? "%02hhX" : "%02hhx" | |
return map { String(format: format, $0) }.joined() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment