How to print unsigned char* in NSLog()

HelmiB picture HelmiB · Nov 23, 2012 · Viewed 25.8k times · Source

Title pretty much says everything.

would like to print (NOT in decimal), but in unsigned char value (HEX). example

unsigned char data[6] = {70,AF,80,1A, 01,7E};
NSLog(@"?",data); //need this output : 70 AF 80 1A 01 7E

Any idea? Thanks in advance.

Answer

rmaddy picture rmaddy · Nov 23, 2012

There is no format specifier for an char array. One option would be to create an NSData object from the array and then log the NSData object.

NSData *dataData = [NSData dataWithBytes:data length:sizeof(data)];
NSLog(@"data = %@", dataData);