12

How can I display the following bytes using NSLog?

const void *devTokenBytes = [devToken bytes];
gonzobrains
  • 7,856
  • 14
  • 81
  • 132
gabac
  • 2,062
  • 6
  • 21
  • 30

2 Answers2

16

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
  • 234,037
  • 30
  • 302
  • 389
Tim
  • 59,527
  • 19
  • 156
  • 165
  • 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 `<` and `>`. 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
  • 6,041
  • 4
  • 27
  • 38
Heath Borders
  • 30,998
  • 16
  • 147
  • 256