Objective C - 通过使用 NSUTF8StringEncoding 编码将数据转换为 NSString 时出现问题

发布于 2024-11-15 15:27:21 字数 698 浏览 1 评论 0原文

我正在尝试

            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 技术交流群。

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

发布评论

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

评论(2

花之痕靓丽 2024-11-22 15:27:21

-[NSString initWithBytes:length:encoding:NSUTF8StringEncoding] 确实适用于长度超过 129 字节的字符串。

你的问题在别处。

编辑回答评论:

正如我所说,您的问题根源不是来自长度,而是来自其他一些问题。正如 @Bvarious 推测的那样,您的字符串可能不是有效的 UTF-8。

以下代码按预期工作:

const char* c_string = "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789";
NSString *string = [[[NSString alloc] initWithBytes:c_string
                                             length:strlen(c_string)
                                           encoding:NSUTF8StringEncoding] autorelease];
assert([string length] == strlen(c_string)); // only valid for ASCII

如果您仍然认为 -[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:

const char* c_string = "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789";
NSString *string = [[[NSString alloc] initWithBytes:c_string
                                             length:strlen(c_string)
                                           encoding:NSUTF8StringEncoding] autorelease];
assert([string length] == strlen(c_string)); // only valid for ASCII

If you still think -[NSString initWithBytes:length:encoding:] might not work as expected, please post code along with the string in question.

北方。的韩爷 2024-11-22 15:27:21

也许您正在将部分 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.

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