NSString 中的原始字节使用什么编码
我需要将 NSData 对象中的一些原始字节存储到 NSString (基本上是空编码)中,但我不确定如何执行此操作。显然,分配不正确的 8 位编码会很糟糕。 NSASCIIStringEncoding
不行,因为文档说“8 位字符内的严格 7 位 ASCII 编码;仅限 ASCII 值 0…127”。但我需要 0x0 - 0xFF 的全范围。
Base64
编码不是可接受的解决方案。
I need to store some raw bytes from NSData
object into an NSString
(basically a null encoding) but I am not sure how to do this. Obviously assigning an improper 8-bit encoding would be bad. NSASCIIStringEncoding
is not OK because the docs say "Strict 7-bit ASCII encoding within 8-bit chars; ASCII values 0…127 only." but I need full range of 0x0 - 0xFF.
Base64
encoding is NOT an acceptable solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
基本上,你不知道。
NSString
用于有效编码的字符串数据的字符串;通常为 UTF8 或 UTF16。NSData
用于任意二进制数据。如果要将原始字节存储到
NSString
中,则需要对它们进行编码,而base64
是最常见的方法之一。Basically, you don't.
An
NSString
is for strings of validly encoded string data; typically UTF8 or UTF16.NSData
is for arbitrary binary data.If you want to store raw bytes into an
NSString
, you need to encode them andbase64
is one of the most common means of doing so.使用 NSNEXTSTEPStringEncoding。根据文档:
它出现在当前文档中(截至撰写本文时),并且在 Apple 和 GNU 的 (OPENSTEP) 标准实现中都可用。
警告:它没有说明这些“扩展”到底是什么,所以要小心。
Use NSNEXTSTEPStringEncoding. According to the documentation:
It appears in the current documentation (as of writing this post) and is available in both Apple and GNU's implementation of the (OPENSTEP) standard.
Caveat: It doesn't state what exactly those "extensions" are, so tread lightly.