0

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

Scott Arciszewski
  • 33,610
  • 16
  • 89
  • 206
Vinayak Bhor
  • 691
  • 1
  • 8
  • 20
  • 1
    That's certainly not a readable UTF8 string? You might first decrypt it. – vadian Apr 10 '19 at 12:14
  • You are right UTF8 encoding not readable but in C# they pass byte array encryptedpassword into GetString() with encoding UTF8.Is there any alternate way in swift ? – Vinayak Bhor Apr 10 '19 at 12:23
  • 1
    As long as the string is not UTF8 compatible you have to deal with raw `Data`. You can convert `[UInt8]` to data simply with `let data = Data(utf8)` – vadian Apr 10 '19 at 12:28
  • Means I need to convert by using String(data: data, encoding: String.Encoding.utf8) – Vinayak Bhor Apr 10 '19 at 12:34
  • Basically yes, but this will fail with your encrypted string, too – vadian Apr 10 '19 at 12:35
  • but how can I pass byte array like [231, 13, 38, 246, 234, 144, 148, 111, 174, 136, 15, 61, 200, 186, 215, 113] to data . – Vinayak Bhor Apr 10 '19 at 12:36
  • As I said `Data(utf8)` but you cannot convert the data to String – vadian Apr 10 '19 at 12:38
  • when I print Data(utf8) on console then I got dataString utf8 : 17 bytes – Vinayak Bhor Apr 10 '19 at 12:46
  • Yes, that's normal. `print(Data(utf8) as NSData)` shows you the raw bytes. – vadian Apr 10 '19 at 12:47
  • Ok. Can I pass this data into String(data: data, encoding: String.Encoding.utf8). Is it worked? – Vinayak Bhor Apr 10 '19 at 12:49
  • 1
    No, you can't. Please read my comments carefully. Once again you can't because it's not an UTF8 string. – vadian Apr 10 '19 at 12:51
  • You are right .Getting nil value – Vinayak Bhor Apr 10 '19 at 12:52
  • then what should I do ? I need to pass String type value to API – Vinayak Bhor Apr 10 '19 at 12:56
  • Is there any alternate way? – Vinayak Bhor Apr 10 '19 at 12:56
  • You could use another encoding. But could it be that the API expects a **base64** encoded string? – vadian Apr 10 '19 at 13:00
  • Data to base64string conversion is possible ? – Vinayak Bhor Apr 10 '19 at 13:04
  • 1
    Yes, of course: `Data(utf8).base64EncodedString()` – vadian Apr 10 '19 at 13:05
  • @vadian I able to convert byte Array to string let inputl: [UInt8] = [37, 213, 90, 210, 131, 170, 64, 10, 244, 100, 199, 109, 113, 60, 7, 173] print("datastring8 : \(String(decoding: inputl, as: UTF8.self))") output is : datastring8 : %�Z҃�@ �d�mq<� (macOS result) – Vinayak Bhor Apr 15 '19 at 12:01
  • online swift compiler with using both windows and macOS both gives different result . Windows result is: % Z҃ @ d mq< server side changes can't be possible I suggest them to use base64 but no help – Vinayak Bhor Apr 15 '19 at 12:01
  • Sorry I can't help because I don't know the specification of the API. – vadian Apr 15 '19 at 12:05
  • Actually same code run for byte array to string conversion in swift but when I tried to run by using online compiler with Windows os result is little difference and I also tried with C# code again shows little different output.I want same result in different os .Your help is appreciated. – Vinayak Bhor Apr 15 '19 at 12:13
  • Hi @vadian Thanks for helping me.I really inspire your approach.I resolved this issued by following your steps and implementing according to that 1.step : decode byte array to String 2.step : convert string to Data 3.step : convert Data to String – Vinayak Bhor Apr 17 '19 at 11:39
  • let datar = String(decoding: MD5ByteArray, as: UTF8.self) let datas = Data(datar.utf8) let datarToString = String(data: datas, encoding: String.Encoding.utf8)! – Vinayak Bhor Apr 17 '19 at 11:42

0 Answers0