NSStream、UTF8String 和 NSString...凌乱的转换
我正在构建一个数据包,通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该字符的正确 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:
.你所拥有的和你想要传输的并不是真正的 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:
uint8_t*
byte array using at least two differentNSString
objects plus the167
code. This will be necessary if the username or password can possibly contain non-ascii characters.NSString
methodgetBytes:maxLength:usedLength:encoding:options:range:remainingRange
and setencoding
toNSASCIIStringEncoding
. If you do this you must validate elsewhere that your username and password is us-ascii only.NSString
methodgetCString
. However, that's been deprecated because you cannot specify the encoding you want.