【问题标题】:How to decode windowsCP1252 encoded string Swift如何解码 windowsCP1252 编码的字符串 Swift
【发布时间】:2019-10-27 23:12:49
【问题描述】:

我的应用程序从 Web 服务接收 JSON 响应。其中一个字段是windowsCP1252 编码字符串。

我正在尝试使用以下代码对其进行解码,

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")")

但是,它无法转换。因此打印失败。

使用以下 kotlin 代码在 android 中转换相同的服务器响应。

val input = "സൗപർണിക"
val op = String(input.toByteArray(charset("Cp1252")), charset("UTF-8"))
println("converted string -----  " + op )

此 kotlin 代码正在正确解码字符串并打印 converted string ----- സൗപർണിക

快速实施有什么问题? 我怎样才能让它发挥作用?

【问题讨论】:

    标签: swift nsstringencoding cp1252


    【解决方案1】:

    问题是1251和1252不一样

    let s = "സൗപർണിക"
    let d = s.data(using: .windowsCP1252)
    let s2 = String(data: d!, encoding: .utf8) // "സൗപർണിക"
    

    【讨论】:

    • 实际上是 1252,我尝试了 1252 和 1251。示例复制自 1251 尝试(更新了我的问题)。 1252 适用于小字符串。但不适用于冗长的字符串
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-25
    • 2011-10-31
    • 2022-12-05
    • 1970-01-01
    相关资源
    最近更新 更多