Skip to content

Instantly share code, notes, and snippets.

@modestman
Created July 27, 2021 06:46
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 modestman/79d5d320fccd5a6127f1c5d38899fa4c to your computer and use it in GitHub Desktop.
Save modestman/79d5d320fccd5a6127f1c5d38899fa4c to your computer and use it in GitHub Desktop.
Hex encoded Data swift
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