将 NSData 转换为 NSString 并忽略空字节? [英] Convert NSData to NSString and ignore null bytes?

查看:81
本文介绍了将 NSData 转换为 NSString 并忽略空字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将 NSData 对象转换为 NSString.这是胡言乱语,但我需要它进行调试.

I need to convert a NSData object to an NSString. It is meant to be gibberish but I need it for debbuging.

当我使用 NSString 的 initWithData 时,它会因为数据有 NULL 字节而中断.如何让它忽略空字节并获得正确的字符串?

When I use NSString's initWithData, it breaks as the data has NULL bytes. How can I make it ignore the null bytes and get a proper string?

推荐答案

逐字节复制 NSData 到 NSMutableData,跳过任何空字节.然后从中初始化字符串.

Copy the NSData to a NSMutableData, byte by byte, skipping any null bytes. Then initialise the string from that.

这篇关于将 NSData 转换为 NSString 并忽略空字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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