yvetterowe yvetterowe - 11 months ago 92
Objective-C Question

Bytes are changed after encoding NSString into NSInputStream via NSData

I run into the following problem when trying encoding an

and then decode from

NSString *inputString = [NSString stringWithFormat:@"%c", 255];
NSData *data = [inputString dataUsingEncoding:NSUTF8StringEncoding];
NSInputStream *stream = [NSInputStream inputStreamWithData:data];
[stream open];
uint8_t bytes;
[stream read:&bytes maxLength:1];
NSLog(@"%i", bytes);

The output is
instead of
. Why?

Answer Source

Because of the kind of encoding you used for the string. UTF-8 is a form of string encoding which will end up converting characters with values above 127 into multi-byte sequences. So although inputString contained a single character, your data object didn't actually contain a single byte as you may have assumed, but multiple (two, in this case) bytes. And when you read from the stream, you only read the first character which happened to be the first byte of the encoding, but not the only one.

You didn't need to run the data through the input stream to see this result. Accessing the first byte of the NSData instance would have shown the same result.

You say that this is a "problem" but you don't suggest what you're trying to actually accomplish. 255 isn't a printable/meaningful text character. If you want to transmit raw data bytes, you can do that directly, rather than using an NSString and string encodings. If you are transmitting strings, then it's already doing the right thing. You just need to be prepared that your data size can exceed your string "length".