'How to decode windowsCP1252 encoded string Swift
From a web service my application receiving a JSON response. In which one field is a windowsCP1252 encoded string.
I am trying to decode it with following code,
let input = "സൗപർണിക"//a string from server response
let data = input.data(using: .windowsCP1252, allowLossyConversion: true)!
print("data \(data.description)")
let output = String(data: data, encoding: .utf8)
print("output \(output ?? "failed")")
But, it is failing to convert. thus printing failed.
Same server response is converted in android with following kotlin code.
val input = "സൗപർണിക"
val op = String(input.toByteArray(charset("Cp1252")), charset("UTF-8"))
println("converted string ----- " + op )
This kotlin code is decoding the string correctly and printing
converted string ----- സൗപർണിക
What is wrong with swift implementation? How can I make it work?
Solution 1:[1]
The problem is that 1251 is not the same as 1252.
let s = "സൗപർണിക"
let d = s.data(using: .windowsCP1252)
let s2 = String(data: d!, encoding: .utf8) // "???????"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | matt |
