Skip to content

Instantly share code, notes, and snippets.

@bricklife
Created August 10, 2017 22:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bricklife/f2ba33f06f7f27609e6f29c1321338cc to your computer and use it in GitHub Desktop.
Save bricklife/f2ba33f06f7f27609e6f29c1321338cc to your computer and use it in GitHub Desktop.
16進数文字列からDataを生成するやつ。もっと綺麗に書けそう #CodePiece
let hexString = "0123456789abcdefABCDEF"
let even = hexString.characters.enumerated().filter { $0.offset % 2 == 0 }.map { $0.element }
let odd = hexString.characters.enumerated().filter { $0.offset % 2 == 1 }.map { $0.element }
let bytes = zip(even, odd).flatMap { UInt8(String([$0.0, $0.1]), radix: 16) }
Data(bytes: bytes) as NSData // <01234567 89abcdef abcdef>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment