Is this short way for printing the string in hex is correct? If not, how it should be fixed?
uint8_t *m = string;
int c = sizeof(string);
while(c--){
printf("%02x ", *(m++));
}
No "oneliner", no. Besides, your code looks broken.
You can't use sizeof like that, you probably mean strlen().
And you need to cast the character to an unsigned type to be safe.
So, something like this, perhaps:
void print_hex(const char *s)
{
while(*s)
printf("%02x", (unsigned int) *s++);
printf("\n");
}
Note that I don't call strlen(), since there's no point in iterating over the string twice when once will do. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With