NSStream,UTF8String& NSString ......凌乱的转换 [英] NSStream, UTF8String & NSString... Messy Conversion

查看:129
本文介绍了NSStream,UTF8String& NSString ......凌乱的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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]];  






所以最后的结果应该看起来像这个......当它首次构建sendData时它会这样做:

USER User§Pass 






然而,当它在服务器端收到时,它看起来很像像这样:

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






...分隔符字符串的长度变为两个,最后一个字母将从命令中被删除。我相信这是UTF8转换的原因。

有人可以为我解释这个问题吗?

Can anyone shed some light on this for me?

非常感谢任何帮助!

推荐答案

这个字符的UTF-8正确编码是两个 - 字节序列0xC2 0xA7,这是你得到的。 ( Fileformat.info 对于这些内容非常宝贵。)这是在LATIN-1集之外,所以你几乎肯定想要使用NSISOLatin1StringEncoding而不是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:.

这篇关于NSStream,UTF8String& NSString ......凌乱的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆