来自服务器的已知 UTF-8 字符串解码不正确

发布于 2025-01-07 02:19:00 字数 809 浏览 3 评论 0原文

在我的应用程序中,我从服务器获取一些字符串值,但最终没有得到正确的字符串。

巴塞з这是来自服务器端的字符串,但我得到的是ءسÙØ·

我尝试在在线解码器中测试响应字符串:

http://www.cafewebmaster.com/online_tools/utf8_encode

它是UTF-8编码的,但我无法在iPhone端解码该字符串。

我查看了这些 Stack Overflow 链接作为参考

将转义的 UTF8 字符转换回其原始形式
objective-c 中的 unicode 转义
utf8_decode for Objective-c

但它们都没有帮助。

In my application, I am getting some string values from a server, but I'm not ending up with the right string.

بسيط this is the string from server side, but what I am getting is بسÙØ·

I tried to test the response string in an online decoder:

http://www.cafewebmaster.com/online_tools/utf8_encode

It is UTF-8 encoded, but I couldn't decode the string on the iPhone side.

I took a look at these Stack Overflow links as reference

Converting escaped UTF8 characters back to their original form
unicode escapes in objective-c
utf8_decode for objective-c

but none of them helped.

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

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

发布评论

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

评论(3

苦行僧 2025-01-14 02:19:00

我从你的问题中不明白以下几点:

  1. 你是否有权访问服务器端(我的意思是它的编程)?
  2. 如何向服务器发送和接收数据?

对于第一个问题,我假设服务器被编程为以 UTF-8 编码向您发送文本。

现在,在 iPhone 上,如果您使用套接字发送到服务器,请使用以下命令:

NSString *messageToSend = @"The text in the language you like";
const uint8_t *str = (uint8_t *) [messageToSend cStringUsingEncoding:NSUTF8StringEncoding];
[self writeToServer:str];

其中函数 writeToServer 是将数据发送到服务器的函数。

如果您愿意将数据放入 SQLite3 数据库中,请使用:

sqlite3_bind_text(statement, 2, [@"The text in the language you like" UTF8String], -1, NULL);

如果您从服务器接收数据(再次使用套接字),请执行以下操作:

[rowData appendBytes:(const void *)buf length:len];
NSString *strRowData = [[NSString alloc] initWithData:rowData encoding:NSUTF8StringEncoding];

我希望这涵盖了您需要的所有情况。

I don't understand from your question the following points:

  1. Do you have access on the server side (I mean the programming of it)?
  2. How do you send and receive data to the server?

For the first question I will assume that the server is programmed to send you text in UTF-8 encoding.

Now on the iPhone if you are sending to the server using sockets use the following:

NSString *messageToSend = @"The text in the language you like";
const uint8_t *str = (uint8_t *) [messageToSend cStringUsingEncoding:NSUTF8StringEncoding];
[self writeToServer:str];

Where the function writeToServer is your function that will send the data to the server.

If you are willing to put the data in a SQLite3 database use:

sqlite3_bind_text(statement, 2, [@"The text in the language you like" UTF8String], -1, NULL);

If you are receiving the data from the server (again using sockets) do the following:

[rowData appendBytes:(const void *)buf length:len];
NSString *strRowData = [[NSString alloc] initWithData:rowData encoding:NSUTF8StringEncoding];

I hope this covers all the cases you need.

我乃一代侩神 2025-01-14 02:19:00

没有任何来源,很难说任何结论性的东西,但在某些时候,您将 UTF-8 编码的字符串解释为 ISO-8859-1,并(错误地)转换将其转换为 UTF-8

字符串 '巴沙尼亚' 的分析:

  • 原始长度:8
  • 逻辑长度:4 个
  • 原始字节:0xD8 0xA8 0xD8 0xB3 0xD9 0x8A 0xD8 0xB7
  • 解释为 ISO-8859-1 (ءسÙØ·):0xC3 0x98 0xC2 0xA8 0xC3 0x98 0xC2 0xB3 0xC3 0x99 0xC2 0x8A 0xC3 0x98 0xC2 0xB7

因此,在某些时候,您可能应该在代码中找到一些对 ISO-8859-1 的引用。找到它并将其删除。

Without any source it is hard to say anything conclusive, but at some point you are interpreting a UTF-8 encoded string as ISO-8859-1, and (wrongfully) converting it to UTF-8:

Analysis for string 'بسيط':

  • raw length: 8
  • logical length: 4
  • raw bytes: 0xD8 0xA8 0xD8 0xB3 0xD9 0x8A 0xD8 0xB7
  • interpreted as ISO-8859-1 (بسÙØ·): 0xC3 0x98 0xC2 0xA8 0xC3 0x98 0xC2 0xB3 0xC3 0x99 0xC2 0x8A 0xC3 0x98 0xC2 0xB7

So at some point you should probably find some reference to ISO-8859-1 in your code. Find it and remove it.

送舟行 2025-01-14 02:19:00

已解决此链接的问题

不同类型的UTF8解码在 NSString

NSString *string = @"ءسÙØ·";

我尝试了

[NSString stringWithUTF8String:(char*)[string cStringUsingEncoding:NSISOLatin1StringEncoding]]

这个方法

谢谢。

SOLVED the issue from this link

Different kind of UTF8 decoding in NSString

NSString *string = @"بسÙØ·";

I tried

[NSString stringWithUTF8String:(char*)[string cStringUsingEncoding:NSISOLatin1StringEncoding]]

this method

Thank You.

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