如何更改 NSString 的编码?

发布于 2024-12-05 07:24:48 字数 901 浏览 0 评论 0原文

我有一个 NStrings 的 NSArray,我在打印数组时从 NSLog 得到了这个。 这是我实现的代码:

NSMetadataQuery *query = [[NSMetadataQuery alloc] init];
.....
NSArray *queryResults = [[query results] copy];

for (NSMetadataItem *item in queryResults)
{
    id value = [item valueForAttribute: kMDItemAlbum];
    [databaseArray addObject: value];
}

"The Chronicles Of Narnia: Prince Caspian",
"Taste the First Love",
"Once (Original Soundtrack)",
"430 West Presents Detroit Calling",
"O\U0308\U00d0\U00b9u\U0301\U00b0\U00aeA\U0300O\U0308A\U0300O\U0308I\U0301A\U030a-O\U0301a\U0300A\U0302\U00a1",
"\U7ea2\U96e8\U6d41\U884c\U7f51",
"I\U0300\U00ab\U00bc\U00abO\U0303A\U030aE\U0300y\U0301\U00b7a\U0301",
"A\U0303n\U0303\U00b8e\U0300\U00b2I\U0300C\U0327U\U0300",
"\U00bb\U00b3A\U0308i\U0302O\U0303\U00bdO\U0301N\U0303",
"American IV (The Man Comes Aro",
"All That We Needed",

现在如何将人类不可读的字符串更改为人类可读的字符串?谢谢。

I have an NSArray of NStrings, I got this from NSLog when printing the array.
Here is the code I have implemented:

NSMetadataQuery *query = [[NSMetadataQuery alloc] init];
.....
NSArray *queryResults = [[query results] copy];

for (NSMetadataItem *item in queryResults)
{
    id value = [item valueForAttribute: kMDItemAlbum];
    [databaseArray addObject: value];
}

"The Chronicles Of Narnia: Prince Caspian",
"Taste the First Love",
"Once (Original Soundtrack)",
"430 West Presents Detroit Calling",
"O\U0308\U00d0\U00b9u\U0301\U00b0\U00aeA\U0300O\U0308A\U0300O\U0308I\U0301A\U030a-O\U0301a\U0300A\U0302\U00a1",
"\U7ea2\U96e8\U6d41\U884c\U7f51",
"I\U0300\U00ab\U00bc\U00abO\U0303A\U030aE\U0300y\U0301\U00b7a\U0301",
"A\U0303n\U0303\U00b8e\U0300\U00b2I\U0300C\U0327U\U0300",
"\U00bb\U00b3A\U0308i\U0302O\U0303\U00bdO\U0301N\U0303",
"American IV (The Man Comes Aro",
"All That We Needed",

Now how can I change the human-unreadable strings to human-readable strings? Thanks.

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

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

发布评论

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

评论(3

能否归途做我良人 2024-12-12 07:24:48

查看 description 完成的转义(例如,\U0308),字符串是错误的(例如,“ÖÐ1ú°®ÀÖÀÖÍÅ-Óà¡”),因为您获得的数据错了。

可能不是Spotlight 的错。 (您可以通过尝试不同的 ID3 标签库来验证这一点。)最有可能的是,文件本身包含编码不良的标签。

要解决此问题:

  1. 使用与字符匹配的 8 位编码对其进行编码。你不能随机选择一个编码(比如“ASCII”,我上次检查时 Cocoa 将其映射到 ISO Latin 1);您需要使用包含输入中所有字符的编码对它们进行正确编码以便您下一步要执行的操作。按顺序尝试 ISO Latin 1、ISO Latin 9、Windows 代码页 1252 和 MacRoman。
  2. 将编码数据解码为 UTF-8。如果失败,请返回步骤 1 并尝试不同的编码。

如果步骤 2 的任何尝试都成功,那么这就是您的有效数据(除非您非常不走运)。如果所有尝试都失败,则数据将无法恢复,您可能需要警告用户他们的输入文件包含虚假标签。

Looking past the escaping done by description (e.g., \U0308), the strings are wrong (e.g., “Öйú°®ÀÖÀÖÍÅ-Óà¡”) because the data you got was wrong.

That's probably not Spotlight's fault. (You could verify that by trying a different ID3-tag library.) Most probably, the files themselves contain poorly-encoded tags.

To fix this:

  1. Encode it in the 8-bit encoding that matches the characters. You can't just pick an encoding (like “ASCII”, which Cocoa mapped to ISO Latin 1 the last time I checked) at random; you need to use the encoding that contains all of the characters in the input and encodes them correctly for what you're going to do next. Try ISO Latin 1, ISO Latin 9, Windows codepage 1252, and MacRoman, in that order.
  2. Decode the encoded data as UTF-8. If this fails, go back to step 1 and try a different encoding.

If step 2 succeeds on any attempt, that is your valid data (unless you're very unlucky). If it fails on all attempts, the data is unrecoverable and you may want to warn the user that their input files contain bogus tags.

凯凯我们等你回来 2024-12-12 07:24:48

解析这些类型的字符串并不是特别容易:请参阅这篇文章了解背景信息。它有其他 SO 帖子的链接,其中包含处理此问题的具体方法。

Parsing these kind of strings aren't particularly easy: See this SO post for background. It's got links to other SO posts with specific ways of handling this problem.

骄傲 2024-12-12 07:24:48

这些字符串是 utf-8 编码的。您可以通过以下方式解码它们:

NSString *myDecoded = [NSString stringWithUTF8String:myEscapedString];

因此,要处理完整的数组“completeArray”,您可以先转换为 const char*,然后再转换回 NSString:

NSMutableArray *processed = [NSMutableArray arrayWithCapacity:completeArray.count];
for (NSString* s in completeArray) {
    [processed addObject:[NSString stringWithUTF8String:[s cStringUsingEncoding:ASCIIEncoding]]];
}

These strings are utf-8 encoded. You can decode them by:

NSString *myDecoded = [NSString stringWithUTF8String:myEscapedString];

So to process your complete array 'completeArray' you can convert to a const char* first and then back into NSString:

NSMutableArray *processed = [NSMutableArray arrayWithCapacity:completeArray.count];
for (NSString* s in completeArray) {
    [processed addObject:[NSString stringWithUTF8String:[s cStringUsingEncoding:ASCIIEncoding]]];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文