是否有任何更短的形式用于以十六进制打印字符串
uint8_t *m = string;
int c = sizeof(string);
while(c--){
printf("%02x ", *(m++));
}
Run Code Online (Sandbox Code Playgroud)
unw*_*ind 18
没有"oneliner",没有.此外,你的代码看起来很糟糕.
你不能这样使用sizeof,你可能意味着strlen().
并且您需要将字符转换为无符号类型才能安全.
所以,这样的事情,或许:
void print_hex(const char *s)
{
while(*s)
printf("%02x", (unsigned int) *s++);
printf("\n");
}
Run Code Online (Sandbox Code Playgroud)
请注意,我没有调用strlen(),因为在一次执行时迭代字符串两次没有意义.:)