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

爷,独闯天下 提交于 2019-12-13 01:43:11

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/9369603/how-to-represent-nsstring-with-336-362-340-value-in-appropriate-encoding-as-n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!