How to display hexadecimal bytes using NSLog

21,092

Solution 1

Assuming that devToken is of type NSData * (from the bytes call), you can use the description method on NSData to get a string containing the hexadecimal representation of the data's bytes. See the NSData class reference.

NSLog(@"bytes in hex: %@", [devToken description]);

Solution 2

If you want a hex sequence:

NSMutableString *hex = [NSMutableString stringWithCapacity:[devToken length]];
for (int i=0; i < [devToken length]; i++) {
  [hex appendFormat:@"%02x", [devToken bytes][i]];
}
Share:
21,092
gabac
Author by

gabac

Updated on July 05, 2022

Comments

  • gabac
    gabac almost 2 years

    How can I display the following bytes using NSLog?

    const void *devTokenBytes = [devToken bytes];
    
  • gabac
    gabac almost 14 years
    but now im getting the error message "warning: passing argument 1 of 'NSLog' from incompatible pointer type"
  • jer
    jer almost 14 years
    Because there's an error in the above code. He left off the @ infront of the string literal. i.e., NSLog("...") instead of NSLog(@"...").
  • Tim
    Tim almost 14 years
    jer, Chuck: thanks for pointing out and fixing, respectively. Too much C for me lately...
  • Deamon
    Deamon over 11 years
    Curious, when I output the hex value, it comes wrapped with < and >. How do I get rid of that?
  • evandrix
    evandrix over 5 years
    (!) Argument type 'const void' is incomplete