将转义的 UTF8 字符转换回其原始形式

发布于 2024-12-11 06:23:11 字数 898 浏览 0 评论 0原文

我正在尝试从来自 plist 的数组中读取字符串并打印这些字符串。

数组中的字符串包含转义的 UTF8 字符 - 例如,从 plist 读取时,“Nuša Florjančič”变为 “Nu\u0161a Florjan\u010di\u010d”。无法更改 plist 的内容,但我的程序需要正确显示名称。

奇怪的是,当我对字符串进行硬编码时,Objective-C 似乎会自动执行此操作。但是,如果我从 plist 中获取字符串,则什么也不会发生。

举个例子,这里有一些代码:

NSString *name1 = @"Nu\u0161a Florjan\u010di\u010d";
NSString *name2 = [list objectAtIndex:0];       
NSLog(@"name 1: %@", name1);
NSLog(@"name 2: %@", name2);

[list objectAtIndex:0] contains @"Nu\u0161a Florjan\u010di\u010d" - 唯一的区别是它有已通过 plist 编辑器设置。

控制台输出是:

2011-10-22 18:00:02.595 Test[13410:11c03] name 1: Nuša Florjančič
2011-10-22 18:00:02.595 Test[13410:11c03] name 2: Nu\u0161a Florjan\u010di\u010d

我尝试了各种方法,包括将字符串转换为 C 字符串,然后使用 UTF-8 编码创建一个 NSString 对象,但没有任何效果。

我真的很感激你的任何指点,可以帮助我解决这个看似平凡的问题。

I'm trying to read strings from an array that's coming from a plist and print those strings.

The strings in the array contain escaped UTF8 characters - for example "Nuša Florjančič" becomes "Nu\u0161a Florjan\u010di\u010d" when read from the plist. There is no way to change the content of the plist, but my program needs to display the names properly.

The strange thing is that Objective-C seems to do this automatically when I'm hardcoding the string. However, if I get the string from the plist nothing happens at all.

To give you an example, here's some code:

NSString *name1 = @"Nu\u0161a Florjan\u010di\u010d";
NSString *name2 = [list objectAtIndex:0];       
NSLog(@"name 1: %@", name1);
NSLog(@"name 2: %@", name2);

[list objectAtIndex:0] contains @"Nu\u0161a Florjan\u010di\u010d" - the only difference is that it has been set via the plist editor.

The console output is:

2011-10-22 18:00:02.595 Test[13410:11c03] name 1: Nuša Florjančič
2011-10-22 18:00:02.595 Test[13410:11c03] name 2: Nu\u0161a Florjan\u010di\u010d

I've tried all sorts of things, including transforming the string into a C-string and then creating an NSString object with a UTF-8 encoding but nothing worked at all.

I'd really appreciate any pointers from you that might help me solve this seemingly mundane problem.

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

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

发布评论

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

评论(2

空袭的梦i 2024-12-18 06:23:11

听起来 plist 中的字符串包含字符“\u0161”而不是 Unicode 字符号 0x161。因此,您需要解码从 plist 中提取的字符串中的 \u 转义符。 NSString 可以使用 NSNonLossyASCIIStringEncoding 为您做到这一点:

#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
    @autoreleasepool {
        NSString *name2escaped = @"Nu\\u0161a Florjan\\u010di\\u010d";
        NSString *name2 = [NSString
            stringWithCString:[name2escaped cStringUsingEncoding:NSUTF8StringEncoding]
            encoding:NSNonLossyASCIIStringEncoding];
        NSLog(@"name2 = %@", name2);
    }
    return 0;
}

It sounds like the string in the plist contains the characters "\u0161" rather than the Unicode character number 0x161. So you need to decode the \u escapes in the string you've extracted from the plist. NSString can do that for you using NSNonLossyASCIIStringEncoding:

#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
    @autoreleasepool {
        NSString *name2escaped = @"Nu\\u0161a Florjan\\u010di\\u010d";
        NSString *name2 = [NSString
            stringWithCString:[name2escaped cStringUsingEncoding:NSUTF8StringEncoding]
            encoding:NSNonLossyASCIIStringEncoding];
        NSLog(@"name2 = %@", name2);
    }
    return 0;
}
怂人 2024-12-18 06:23:11

其他解决方案是解析您的列表字符串(我曾经在构建它之前解析它)

NSString yourFinalString = [NSString stringWithCString:[yourOriginalString cStringUsingEncoding:NSISOLatin1StringEncoding] encoding:NSUTF8StringEncoding];

(看起来像克罗地亚语,我认为 latin1 会正确适合)

Other solution is parse your list string (I used to parse it before build it)

NSString yourFinalString = [NSString stringWithCString:[yourOriginalString cStringUsingEncoding:NSISOLatin1StringEncoding] encoding:NSUTF8StringEncoding];

(Looks like Croatian, i think latin1 will fit properly)

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