Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print unsigned char* in NSLog()

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.

like image 344
HelmiB Avatar asked Nov 23 '12 04:11

HelmiB


2 Answers

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);
like image 172
rmaddy Avatar answered Sep 28 '22 02:09

rmaddy


Nothing in the standard libraries will do it, so you could write a small hex dump function, or you could use something else that prints non-ambigious full data. Something like:

char buf[1 + 3*dataLength];
strvisx(buf, data, dataLength, VIS_WHITE|VIS_HTTPSTYLE);
NSLog(@"data=%s", buf);

For smallish chunks of data you could try to make a NSData and use the debugDescription method. That is currently a hex dump, but nothing promises it will always be one.

like image 21
Stripes Avatar answered Sep 28 '22 01:09

Stripes