Skip to content

Instantly share code, notes, and snippets.

@khanlou
Created November 15, 2016 16:16
Show Gist options
  • Select an option

  • Save khanlou/d248402a8fd960304b96db87b062ed7e to your computer and use it in GitHub Desktop.

Select an option

Save khanlou/d248402a8fd960304b96db87b062ed7e to your computer and use it in GitHub Desktop.
import Foundation
extension Data {
var hexString1: String {
return self.map({ return String(format: "%02hhx", $0) }).joined()
}
var hexString2: String {
return self.reduce("", { return $0 + String(format: "%02hhx", $1) })
}
var hexString3: String {
return self.reduce("", { String(format: "\($0)%02hhx", $1) })
}
}
let token = "test data".data(using: String.Encoding.utf8)!
token.hexString3 == "746573742064617461" // => true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment