Given a hexadecimal string in Swift, convert to hex value

后端 未结 4 712
难免孤独
难免孤独 2021-01-06 18:30

Suppose I am given a string like this:

D7C17A4F

How do I convert each individual character to a hex value?

So D

4条回答
  •  囚心锁ツ
    2021-01-06 19:01

    My variation of @martin-r answer:

    extension String {
    
        func hexToByteArray() -> [UInt8] {
            let byteCount = self.utf8.count / 2
            var array = [UInt8](count: byteCount, repeatedValue: 0)
            var from = self.startIndex
            for i in 0..

提交回复
热议问题