Reputation: 5053
I have String swift 5. string is like bellow..
Kawran Bazar FDC Road, East Nakhalpara, নিà¦à§à¦¤à¦¨, ঢাà¦à¦¾, ঢাà¦à¦¾ বিà¦à¦¾à¦, বাà¦à¦²à¦¾à¦¦à§à¦¶
I need to convert it. I tried it like bellow
let data = location_name.data(using: .utf8)
var str = String(decoding:data!, as: UTF8.self)
print("location_name***",str)
But need to convert string bellow like from above
Kawran Bazar FDC Road, East Nakhalpara, নিকেতন, ঢাকা, ঢাকা বিভাগ, বাংলাদেশ
How to solve the problem
Upvotes: 3
Views: 7324
Reputation: 299265
This is the output you get if at some point UTF-8 data was interpreted as Latin1. For example:
let s = "Kawran Bazar FDC Road, East Nakhalpara, নিকেতন, ঢাকা, ঢাকা বিভাগ, বাংলাদেশ"
let utf8Data = Data(s.utf8)
let latin1 = String(data: utf8Data, encoding: .isoLatin1)!
print(latin1)
==>
Kawran Bazar FDC Road, East Nakhalpara, নিà¦à§à¦¤à¦¨, ঢাà¦à¦¾, ঢাà¦à¦¾ বিà¦à¦¾à¦, বাà¦à¦²à¦¾à¦¦à§à¦¶
You should first try to fix this and remove this incorrect string creation. If you cannot, then you can round-trip it back through Latin1.
let latin1Data = latin1.data(using: .isoLatin1)!
let utf8String = String(data: latin1Data, encoding: .utf8)!
utf8String == s // true
Upvotes: 6