NSStream、UTF8String 和 NSString...凌乱的转换

发布于 2024-07-24 22:29:27 字数 870 浏览 7 评论 0原文

我正在构建一个数据包,通过 NSStream 发送到服务器。 我试图用“§”(ascii 代码 167)分隔两条数据。 这就是服务器的构建方式,所以我需要尝试保持在这些范围内...

unichar asciiChar = 167;  //yields @"§"
[self setSepString:[NSString stringWithCharacters:&asciiChar length:1]]; 

sendData=[NSString stringWithFormat:@"USER User%@Pass", sepString];

NSLog(sendData);

const uint8_t *rawString=(const uint8_t *)[sendData UTF8String];

[oStream write:rawString maxLength:[sendData length]];  

所以最终结果应该如下所示..并且在第一次构造 sendData 时会这样做:

USER User§Pass 

但是,当服务器端接收到它时,它看起来像这样:

//not a direct copy and paste. The 'mystery character' may not be exact
USER UserˤPas

...分隔符字符串的长度变成了两个,并且最后一个字母从命令中被裁剪掉。 我相信这是由 UTF8 转换引起的。

任何人都可以帮我解释一下吗?

任何帮助将不胜感激!

I am constructing a data packet to be sent over NSStream to a server. I am trying to seperate two pieces of data with the a '§' (ascii code 167). This is the way the server is built, so I need to try to stay within those bounds...

unichar asciiChar = 167;  //yields @"§"
[self setSepString:[NSString stringWithCharacters:&asciiChar length:1]]; 

sendData=[NSString stringWithFormat:@"USER User%@Pass", sepString];

NSLog(sendData);

const uint8_t *rawString=(const uint8_t *)[sendData UTF8String];

[oStream write:rawString maxLength:[sendData length]];  

So the final outcome should look like this.. and it does when sendData is first constructed:

USER User§Pass 

however, when it is received on the server side, it looks like this:

//not a direct copy and paste. The 'mystery character' may not be exact
USER UserˤPas

...the seperator string has become two in length, and the last letter is getting cropped from the command. I believe this to be cause by the UTF8 conversion.

Can anyone shed some light on this for me?

Any help would be greatly appreciated!

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

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

发布评论

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

评论(2

随遇而安 2024-07-31 22:29:27

该字符的正确 UTF-8 编码是两字节序列 0xC2 0xA7,这就是您所得到的。 (Fileformat.info 对于这些东西来说是无价的。)这是在 LATIN-1 集中,因此您几乎肯定希望使用 NISOLatin1StringEncoding 而不是 NSUTF8StringEncoding 以获得单字节 167 编码。 看看NSString -dataUsingEncoding:

The correct encoding in UTF-8 for this character is the two-byte sequence 0xC2 0xA7, which is what you're getting. (Fileformat.info is invaluable for this stuff.) This is out of the LATIN-1 set, so you almost certainly want to be using NSISOLatin1StringEncoding rather than NSUTF8StringEncoding in order to get a single-byte 167 encoding. Look at NSString -dataUsingEncoding:.

行至春深 2024-07-31 22:29:27

你所拥有的和你想要传输的并不是真正的 UTF-8 字符串,而且从技术上讲它也不是 us-ascii,因为它只有 7 位。 您想要根据您正在使用的协议传输任意字节数组。 字节数组的两个字段(用户名和密码)本身可能是 UTF-8 字符串,但使用 167 分隔符时,它不能是 UTF-8 字符串。

以下是我看到的一些选项:

  • 使用至少两个不同的 NSString 对象加上 167 代码构造 uint8_t* 字节数组。 如果用户名或密码可能包含非 ASCII 字符,则这是必要的。
  • 使用 NSString 方法 getBytes:maxLength:usedLength:encoding:options:range:remainingRange 并将 encoding 设置为 NSASCIIStringEncoding 。 如果您这样做,您必须在其他地方验证您的用户名和密码是否仅限 us-ascii。
  • 使用NSString方法getCString。 但是,这已被弃用,因为您无法指定所需的编码。

What you have and what you want to transmit is not really a UTF-8 string, and it's technically not us-ascii, because that's only 7 bits. You want to transmit an arbitrary array of bytes, according to the protocol that you're working with. The two fields of the byte array, username and password, might themselves be UTF-8 strings, but with the 167 separator it cannot be a UTF-8 string.

Here are some options I see:

  • Construct the uint8_t* byte array using at least two different NSString objects plus the 167 code. This will be necessary if the username or password can possibly contain non-ascii characters.
  • Use the NSString method getBytes:maxLength:usedLength:encoding:options:range:remainingRange and set encoding to NSASCIIStringEncoding. If you do this you must validate elsewhere that your username and password is us-ascii only.
  • Use the NSString method getCString. However, that's been deprecated because you cannot specify the encoding you want.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文