Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert array of bytes [UInt8] into hexa string in Swift

Tags:

swift

I found how to convert hexa string into bytes [UInt8] but I have not found how to convert bytes [UInt8] into an hexa string in Swift

this hexstring convert to string code:

static func bytesConvertToHexstring(byte : [UInt8]) -> String {
    var string = ""

    for val in byte {
        //getBytes(&byte, range: NSMakeRange(i, 1))
        string = string + String(format: "%02X", val)
    }

    return string
}

samething like this result:

"F063C52A6FF7C8904D3F6E379EB85714ECA9C1CB1E8DFD6CA5D3B4A991269D60F607C565C327BD0ECC0985F74E5007E0D276499E1ADB4E0C92D8BDBB46E57705B2D5390FF5CBD4ED1B850C537301CA7E"

UInt8 array: [0, 11, 8, 15, 6, 6, 5, 8, 8, 4, 14, 14, 0, 0, 9, 12, 6, 4, 10, 6, 4, 8, 6, 2, 14, 2, 6, 13, 3, 3, 12, 4, 3, 12, 8, 13, 14, 4, 10, 1, 12, 15, 4, 0, 14, 14, 0, 8, 8, 14, 6, 15, 2, 2, 9, 15, 13, 6, 2, 6, 8, 15, 4, 2, 12, 1, 0, 13, 13, 4, 6, 0, 9, 6, 8, 2, 7, 0, 6, 1, 3, 3, 9, 15, 5, 7, 12, 8, 7, 5, 13, 14, 15, 6, 7, 6, 12, 6, 7, 7, 11, 9, 6, 0, 14, 5, 6, 14, 1, 5, 13, 10, 12, 13, 14, 2, 13, 14, 4, 7, 13, 0, 3, 10, 6, 11, 9, 12, 7, 11, 5, 3, 5, 11, 4, 9, 6, 10, 14, 0, 11, 7, 15, 9, 3, 14, 5, 1, 10, 14, 5, 6, 12, 4, 12, 14, 4, 3, 9, 8, 0]

like image 296
Clever Avatar asked Nov 05 '15 14:11

Clever


3 Answers

Xcode 11 • Swift 5.1 or later

extension StringProtocol {
    var hexa: [UInt8] {
        var startIndex = self.startIndex
        return (0..<count/2).compactMap { _ in
            let endIndex = index(after: startIndex)
            defer { startIndex = index(after: endIndex) }
            return UInt8(self[startIndex...endIndex], radix: 16)
        }
    }
}

extension DataProtocol {
    var data: Data { .init(self) }
    var hexa: String { map { .init(format: "%02x", $0) }.joined() }
}

"0f00ff".hexa                 // [15, 0, 255]
"0f00ff".hexa.data            // 3 bytes
"0f00ff".hexa.data.hexa       // "0f00ff"
"0f00ff".hexa.data as NSData  // <0f00ff>

Note: Swift 4 or 5 syntax click here

like image 83
Leo Dabus Avatar answered Nov 06 '22 08:11

Leo Dabus


Thanks to Brian for his routine. It could conveniently be added as a Swift extension as below.

 extension Array where Element == UInt8 {
  func bytesToHex(spacing: String) -> String {
    var hexString: String = ""
    var count = self.count
    for byte in self
    {
        hexString.append(String(format:"%02X", byte))
        count = count - 1
        if count > 0
        {
            hexString.append(spacing)
        }
    }
    return hexString
}

}

Example of call:

let testData: [UInt8] = [15, 0, 255]
print(testData.bytesToHex(spacing: " "))          // 0F 00 FF
like image 32
Daniel Mavrakis Avatar answered Nov 06 '22 08:11

Daniel Mavrakis


One liner:

testData.map{ String(format:"%02X", $0) }.joined(separator: " ")
like image 3
Holger Bille Avatar answered Nov 06 '22 08:11

Holger Bille