How to Create a String from Utf8 in Swift

How to convert string to unicode(UTF-8) string in Swift?

Use this code,

let str = String(UTF8String: strToDecode.cStringUsingEncoding(NSUTF8StringEncoding))

hope its helpful

Decode string using UTF-8 in swift 5

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

How convert string to utf-8? (Swift, Alamofire)

Swift 3

let newStr = String(utf8String: stringToDecode.cString(using: .utf8)!)

Source StackOverFlow

Swift 3 method to create utf8 encoded Data from String

It's simple:

let input = "Hello World"
let data = input.data(using: .utf8)!

If you want to terminate data with null, simply append a 0 to it. Or you may call cString(using:)

let cString = input.cString(using: .utf8)! // null-terminated

Can the conversion of a String to Data with UTF-8 encoding ever fail?

UTF-8 can represent all valid Unicode code points, therefore a conversion
of a Swift string to UTF-8 data cannot fail.

The forced unwrap in

let string = "some string .."
let data = string.data(using: .utf8)!

is safe.

The same would be true for .utf16 or .utf32, but not for
encodings which represent only a restricted character set,
such as .ascii or .isoLatin1.

You can alternatively use the .utf8 view of a string to create UTF-8 data,
avoiding the forced unwrap:

let string = "some string .."
let data = Data(string.utf8)

Converting any UTF-8 encoded Characters in String in Swift from API

You can use NSAttributedString to convert these HTML entities to string.

let htmlString = "test北京的test"
if let htmldata = htmlString.dataUsingEncoding(NSUTF8StringEncoding), let attributedString = try? NSAttributedString(data: htmldata, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil) {
let finalString = attributedString.string
print(finalString)
//output: test北京的test
}

Swift UTF8 encoding and non UTF8 character

I've found a solution.

The UTF8 take 8 bit of table ASCII, and the UTF16 take 16 bit ASCII table, the solution is simple by modifying my function to:

func stringToUTF16String (stringaDaConvertire stringa: String) -> String {
let encodedData = stringa.dataUsingEncoding(NSUTF16StringEncoding)!
let attributedOptions = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType]
let attributedString = NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil, error: nil)!
//println(attributedString.string)
return attributedString.string
}


Related Topics



Leave a reply



Submit