How to convert Data to hex string in swift

前端 未结 7 2243
走了就别回头了
走了就别回头了 2020-11-22 07:59

I want the hexadecimal representation of a Data value in Swift.

Eventually I\'d want to use it like this:

let data = Data(base64Encoded: \"aGVsbG8gd2         


        
7条回答
  •  余生分开走
    2020-11-22 08:24

    This code extends the Data type with a computed property. It iterates through the bytes of data and concatenates the byte's hex representation to the result:

    extension Data {
        var hexDescription: String {
            return reduce("") {$0 + String(format: "%02x", $1)}
        }
    }
    

提交回复
热议问题