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]];
}
Author by
gabac
Updated on July 05, 2022Comments
-
gabac almost 2 years
How can I display the following bytes using NSLog?
const void *devTokenBytes = [devToken bytes];
-
gabac almost 14 yearsbut now im getting the error message "warning: passing argument 1 of 'NSLog' from incompatible pointer type"
-
jer almost 14 yearsBecause there's an error in the above code. He left off the @ infront of the string literal. i.e., NSLog("...") instead of NSLog(@"...").
-
Tim almost 14 yearsjer, Chuck: thanks for pointing out and fixing, respectively. Too much C for me lately...
-
Deamon over 11 yearsCurious, when I output the hex value, it comes wrapped with
<
and>
. How do I get rid of that? -
evandrix over 5 years(!)
Argument type 'const void' is incomplete