Created
August 10, 2017 22:16
-
-
Save bricklife/95c679f7b44feb6e754047cae4a03d0d to your computer and use it in GitHub Desktop.
16進数文字列からDataを生成するString.Index版。たぶんさっきのと同じ結果 #CodePiece
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
let hexString = "0123456789abcdefABCDEF" | |
var bytes = [UInt8]() | |
var index = hexString.startIndex | |
while let i = hexString.index(index, offsetBy: 2, limitedBy: hexString.endIndex) { | |
let byteLiteral = hexString.substring(with: index..<i) | |
guard let byte = UInt8(byteLiteral, radix: 16) else { continue } | |
bytes.append(byte) | |
index = i | |
} | |
Data(bytes: bytes) as NSData |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment