Convert NSData bytes to NSString?

That’s an important point that should be re-emphasized I think. It turns out that,

NSString *content = [NSString stringWithUTF8String:[responseData bytes]];

is not the same as,

NSString *content = [[NSString alloc]  initWithBytes:[responseData bytes]
              length:[responseData length] encoding: NSUTF8StringEncoding];

the first expects a NULL terminated byte string, the second doesn’t. In the above two cases content will be NULL in the first example if the byte string isn’t correctly terminated.

Leave a Comment