如何用 '\336\362\340' 表示 NSString值以适当的编码作为普通文本?

发布于 2025-01-07 19:47:01 字数 502 浏览 1 评论 0原文

我从音频流中得到这样的字符串作为标题:

Þòà - Ïàäàòü

我知道这个字符串是俄语的。我需要在 UILabel 中正确显示它。 我尝试这样做:

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

现在 goodValue 包含下一个值:

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

我看到原始保存的字符数。但是我应该如何将其转换为普通字符串以用作 UILabel 中的文本?

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

诗笺 2025-01-14 19:47:01

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

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

 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
};

现在让我们迭代并显示所有可能的结果:

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);
        }
    }
}

之后我查看结果并找到了好的字符串。这就是全部 =)

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文