如何用适当的编码中的

如何用适当的编码中的

本文介绍了如何用适当的编码中的'\336\362\340'值代表NSString作为普通文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到保存原始的字符数。但是我应该如何将它转换成正常的字符串,以便在UILabel中使用文本?



提前感谢

解决方案

我写了一些代码来迭代和记录所有可能的组合。



起初我在NSString.h中找到了所有可能的编码列表并将其设置为C数组可能的编码:

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

现在让我们重复一遍并显示所有可能的结果:

  int numberOfEncodings = 23; (int i = 0; i< numberOfEncodings; i ++)

{
NSLog(@===============%d == ===========,编码[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:%@,编码[j],str);
}
}
}

之后,我查看结果并发现好字符串。这就是全部=)



注意:所有编码都是NSStringEncoding枚举的值。你可以认为你可以从0迭代到编码的数量,而不是定义编码[]数组。但是您不应该这样做,因为编码值不是上升的int。例如NSMacOSRomanStringEncoding = 30,其中一些编码是另一个的别名。而不是更好地定义可能编码的数组。


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:

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.

解决方案

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.

这篇关于如何用适当的编码中的'\336\362\340'值代表NSString作为普通文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 23:21