What encoding to use with raw bytes in a NSString
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.
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 and base64
is one of the most common means of doing so.
Use NSNEXTSTEPStringEncoding. According to the documentation:
8-bit ASCII encoding with NEXTSTEP extensions.
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.
精彩评论