Skip to content

Instantly share code, notes, and snippets.

@bricklife
Created August 10, 2017 22:16
Show Gist options
  • Save bricklife/95c679f7b44feb6e754047cae4a03d0d to your computer and use it in GitHub Desktop.
Save bricklife/95c679f7b44feb6e754047cae4a03d0d to your computer and use it in GitHub Desktop.
16進数文字列からDataを生成するString.Index版。たぶんさっきのと同じ結果 #CodePiece
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