|
@@ -59,54 +59,4 @@ extension String {
|
|
|
return try self.utf8.lazy.map({ $0 as UInt8 }).authenticate(with: authenticator).toHexString()
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- private static var unicodeHexMap:[UnicodeScalar:UInt8] = ["0":0,"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":7,"8":8,"9":9,"a":10,"b":11,"c":12,"d":13,"e":14,"f":15,"A":10,"B":11,"C":12,"D":13,"E":14,"F":15]
|
|
|
- /**
|
|
|
- Converts the a hexadecimal string to a corresponding array of bytes.
|
|
|
-
|
|
|
- So the string "ff00" would get converted to [255, 0].
|
|
|
- Supports odd number of characters by interpreting the last character as its' hex value ("f" produces [16] as if it was "0f").
|
|
|
-
|
|
|
- - Returns: An array of 8 bit unsigned integers (bytes).
|
|
|
- */
|
|
|
- public func hexToBytes() -> Array<UInt8>{
|
|
|
- var bytes:Array<UInt8> = []
|
|
|
- do{
|
|
|
- try self.streamHexBytes { (byte) in
|
|
|
- bytes.append(byte)
|
|
|
- }
|
|
|
- }catch _{
|
|
|
- return []
|
|
|
- }
|
|
|
-
|
|
|
- return bytes
|
|
|
- }
|
|
|
-
|
|
|
- // Allows str.hexToBytes() and Array<UInt8>(hex: str) to share the same code
|
|
|
- // Old functionality returns a blank array upon encountering a non hex character so this method must throw in order to replicate that in methods relying on this method. (ex. "ffn" should return [] instead of [255])
|
|
|
- // Changed NSError to custom Error enum
|
|
|
- public func streamHexBytes(_ block:(UInt8)->Void) throws{
|
|
|
- var buffer:UInt8?
|
|
|
- var skip = hasPrefix("0x") ? 2 : 0
|
|
|
- for char in unicodeScalars.lazy {
|
|
|
- guard skip == 0 else {
|
|
|
- skip -= 1
|
|
|
- continue
|
|
|
- }
|
|
|
- guard let value = String.unicodeHexMap[char] else {
|
|
|
- throw HexError.invalidCharacter
|
|
|
- }
|
|
|
- if let b = buffer {
|
|
|
- block(b << 4 | value)
|
|
|
- buffer = nil
|
|
|
- } else {
|
|
|
- buffer = value
|
|
|
- }
|
|
|
- }
|
|
|
- if let b = buffer{block(b)}
|
|
|
- }
|
|
|
-
|
|
|
- private enum HexError:Error {
|
|
|
- case invalidCharacter
|
|
|
- }
|
|
|
}
|