How to convert byte array to string in swift 3

1.3k Views Asked by At

I referred C# code for byte array to string conversion in swift

System.Text.Encoding.UTF8.GetString(encryptedpassword)

I tried to convert byte array to string by using below few codes:

(A)

 let utf8 : [UInt8] = [231, 13, 38, 246, 234, 144, 148, 111, 174, 136, 15, 61, 200, 186, 215, 113,0]
 let str = NSString(bytes: utf8, length: utf8.count, encoding: String.Encoding.utf8.rawValue)
 print("str : \(str)")

result : getting nil value

(B)

 let datastring = NSString(bytes: chars, length: count, encoding: String.Encoding.utf8.rawValue)
 print("string byte data\(chars.map{"\($0)"}.reduce(""){$0+$1})")

result : 23113382462341441481111741361561200186215113 (I thought this is not ideal way)

I searched from last two days and tried other multiple ways but I missed something or doing some mistake . Please help me out resolved this issue.

Referred links: how-to-convert-uint8-byte-array-to-string-in-swift

how-to-convert-uint8-byte-array-to-string-in-swift 2

0

There are 0 best solutions below