Iterate through NSData bytes

cocoa-touch, nsdata, objective-c

Solution

Rather than appending bytes to a mutable string, create a string using the data:

// Be sure to use the right encoding:
NSString *result = [[NSString alloc] initWithData:myData encoding:NSUTF8StringEncoding];

If you really want to loop through the bytes:

NSMutableString *result = [NSMutableString string];
const char *bytes = [myData bytes];
for (int i = 0; i < [myData length]; i++)
{
    [result appendFormat:@"%02hhx", (unsigned char)bytes[i]];
}

Problem

How can I iterate through `[NSData bytes]` one by one and append them to an `NSMutableString` or print them using `NSLog()`?

Original source