Reputation: 12819
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?
Upvotes: 1
Views: 721
Reputation: 534893
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) // "സൗപർണിക"
Upvotes: 2