|
@@ -23,3 +23,18 @@ extension Array {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+extension Array where Element: Integer, Element.IntegerLiteralType == UInt8 {
|
|
|
+ public init(hex: String) {
|
|
|
+ self.init()
|
|
|
+
|
|
|
+ let utf8 = Array<Element.IntegerLiteralType>(hex.utf8)
|
|
|
+ let skip0x = hex.hasPrefix("0x") ? 2 : 0
|
|
|
+ for idx in stride(from: utf8.startIndex.advanced(by: skip0x), to: utf8.endIndex, by: utf8.startIndex.advanced(by: 2)) {
|
|
|
+ let byteHex = "\(UnicodeScalar(utf8[idx]))\(UnicodeScalar(utf8[idx.advanced(by: 1)]))"
|
|
|
+ if let byte = UInt8(byteHex, radix: 16) {
|
|
|
+ self.append(byte as! Element)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|