12

How can I display the following bytes using NSLog?

const void *devTokenBytes = [devToken bytes];
gonzobrains
  • 7,506
  • 13
  • 77
  • 129
gabac
  • 2,012
  • 6
  • 21
  • 30

2 Answers2

15

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]);
Chuck
  • 228,856
  • 29
  • 291
  • 386
Tim
  • 58,719
  • 19
  • 158
  • 163
  • but now im getting the error message "warning: passing argument 1 of 'NSLog' from incompatible pointer type" – gabac Sep 14 '10 at 17:00
  • 4
    Because there's an error in the above code. He left off the @ infront of the string literal. i.e., NSLog("...") instead of NSLog(@"..."). – jer Sep 14 '10 at 17:07
  • jer, Chuck: thanks for pointing out and fixing, respectively. Too much C for me lately... – Tim Sep 14 '10 at 22:29
  • Curious, when I output the hex value, it comes wrapped with ``. How do I get rid of that? – pixelfreak Mar 26 '13 at 22:45
9

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]];
}
evandrix
  • 5,889
  • 4
  • 26
  • 35
Heath Borders
  • 29,483
  • 16
  • 137
  • 246