Search code examples
objective-cencodingnsstringnsstringencoding

How to represent NSString with '\336\362\340' value in appropriate encoding as normal text?


I got strings like this from audio stream as titles:

Þòà - Ïàäàòü

I know that this string in russian. And I need to show it correctly in UILabel. I try this:

NSData *data = [value dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *goodValue = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

Now goodValue contains next value:

\336\362\340 - \317\340\344\340\362\374

Number of characters as I see the save with original. But how I should convert it into normal string for using as text in UILabel?

Thanks in advance.


Solution

  • I wrote some code for iterating and logging all possible combinations.

    At first I found list of all possible encodings in NSString.h and set it to C array of possible encodings:

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

    And now let's iterate and show all possible results:

    int numberOfEncodings = 23;
    
    for (int i = 0; i < numberOfEncodings; i++) {
        NSLog(@"=============== %d =============", encodings[i]);
        constchar *asd = [value cStringUsingEncoding:encodings[i]];
        if (asd == NULL) {
            NSLog(@"asd == NULL");
        } else {
            for (int j = 0; j < numberOfEncodings; j++) {
                NSString *str = [NSStringstringWithCString:asd encoding:encodings[j]];
                NSLog(@"%d: %@", encodings[j], str);
            }
        }
    }
    

    After that I look through results and found good string. That's all =)

    note: all encodings are values of NSStringEncoding enum. And you could think that you could iterate from 0 to number of encodings instead of defining encodings[] array. But you shouldn't do this, because encoding values are not ascending ints. For example NSMacOSRomanStringEncoding = 30 and some of this encoding are aliases for another. Than better to define array of possible encodings.