Decode UTF8 symbols

205 Views Asked by At

I have a string in swift:

let flag = "Cattì ò"

I am trying to convert the UTF8 symbols.

I have tried using

stringByRemovingPercentEncoding

but noting changes. How can I convert the symbols properly ?

1

There are 1 best solutions below

0
Code Different On BEST ANSWER

Welcome to the encoding guessing game! Look like somewhere along the pathway, your string didn't get the correct code page. Here's one way to guess it:

let flag = "Cattì ò"

let encodings = [NSASCIIStringEncoding,
    NSNEXTSTEPStringEncoding,
    NSJapaneseEUCStringEncoding,
    NSUTF8StringEncoding,
    NSISOLatin1StringEncoding,
    NSSymbolStringEncoding,
    NSNonLossyASCIIStringEncoding,
    NSShiftJISStringEncoding,
    NSISOLatin2StringEncoding,
    NSUnicodeStringEncoding,
    NSWindowsCP1251StringEncoding,
    NSWindowsCP1252StringEncoding,
    NSWindowsCP1253StringEncoding,
    NSWindowsCP1254StringEncoding,
    NSWindowsCP1250StringEncoding,
    NSISO2022JPStringEncoding,
    NSMacOSRomanStringEncoding,
    NSUTF16StringEncoding,
    NSUTF16BigEndianStringEncoding,
    NSUTF16LittleEndianStringEncoding,
    NSUTF32StringEncoding,
    NSUTF32BigEndianStringEncoding,
    NSUTF32LittleEndianStringEncoding]

for encoding in encodings {
    if let bytes = flag.cStringUsingEncoding(encoding),
        flag_utf8 = String(CString: bytes, encoding: NSUTF8StringEncoding) {
        print("\(encoding): \(flag_utf8)")
    }
}

The array contains all the encodings that Cocoa supports.

From the results, it seems like your string was encoded in NSISOLatin1StringEncoding (a.k.a ISO-8859-1), the default encoding for HTML 4.01. This gives Cattì ò in UTF-8, not exactly match your desired result but is the closest among all code pages.

Other good candidates are NSWindowsCP1252StringEncoding and NSWindowsCP1254StringEncoding so I'd suggest you check with other strings.