Objective C - 通过使用 NSUTF8StringEncoding 编码将数据转换为 NSString 时出现问题
我正在尝试
uint8_t buf[1024];
unsigned int len = 0;
len = [(NSInputStream *)stream read:buf maxLength:1024];
if(len) {
使用以下命令读取从 iPhone 上的 Java 服务器发送的 UTF-8 数据:
NSString *chrStr = [[NSString alloc] initWithBytes:(const void *)buf length:len encoding:NSUTF8StringEncoding];
when buf has length < 129,转换就可以了。但如果长度> 129, chrStr 返回 null
如果我通过 NSASCIIStringEncoding 更改编码:
NSString *chrStr = [[NSString alloc] initWithBytes:(const void *)buf length:len encoding:NSASCIIStringEncoding];
转换成功,长度 > 129 但 UTF8 字符不正确:(
对不起我的英语。
I'm trying to read UTF-8 data sent from Java server on iphone
uint8_t buf[1024];
unsigned int len = 0;
len = [(NSInputStream *)stream read:buf maxLength:1024];
if(len) {
with this :
NSString *chrStr = [[NSString alloc] initWithBytes:(const void *)buf length:len encoding:NSUTF8StringEncoding];
when buf has length < 129 , the conversion is ok. but if length > 129, chrStr is return null
If i Change encoding by NSASCIIStringEncoding :
NSString *chrStr = [[NSString alloc] initWithBytes:(const void *)buf length:len encoding:NSASCIIStringEncoding];
the conversion is ok with length > 129 but UTF8 characters is incorrectly :(
sorry for my Enghlish.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
-[NSString initWithBytes:length:encoding:NSUTF8StringEncoding]
确实适用于长度超过 129 字节的字符串。你的问题在别处。
编辑回答评论:
正如我所说,您的问题根源不是来自长度,而是来自其他一些问题。正如 @Bvarious 推测的那样,您的字符串可能不是有效的 UTF-8。
以下代码按预期工作:
如果您仍然认为 -[NSString initWithBytes:length:encoding:] 可能无法按预期工作,请与有问题的字符串一起发布代码。
-[NSString initWithBytes:length:encoding:NSUTF8StringEncoding]
does work with strings longer than 129 bytes.You problem is elsewhere.
Edit to answer comment:
As I said, you problem origins not from the length but from some other issue. As @Bavarious speculated, your string might not be valid UTF-8.
The following code works as expected:
If you still think -[NSString initWithBytes:length:encoding:] might not work as expected, please post code along with the string in question.
也许您正在将部分 UTF8 字符串读入“buf”。即,在下次从流中读取时,您可能会得到一些尾随字节,因为您的 UTF-8 编码字符已在中间分割。
Perhaps you're reading a partial UTF8 string into "buf". I.e., on the next read from the stream you might get some trailing bytes because your UTF-8 encoded character has been split in the middle.