【发布时间】:2014-03-13 23:37:32
【问题描述】:
在下面的代码中,mac_str 是字符指针,mac 是uint8_t 数组:
sscanf(mac_str,"%x:%x:%x:%x:%x:%x",&mac[0],&mac[1],&mac[2],&mac[3],&mac[4],&mac[5]);
当我尝试上面的代码时,它给了我一个警告:
warning: format ‘%x’ expects argument of type ‘unsigned int *’, but argument 8 has type ‘uint8_t *’ [-Wformat]
但我在他们指定的一些代码中看到了
sscanf(str,"%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",&mac[0],&mac[1],&mac[2],&mac[3],&mac[4],&mac[5]);
它没有给出任何警告,但两者的工作方式相同。
使用 hhx 而不仅仅是 x 有什么需要?
【问题讨论】:
-
hh代表历史猜测,代表 一半。也就是一个字节。 -
使用
sscanf("%" SCNx8 , &x); -
@Joseph 我没有说它是重复的。但是,OP 可能会在其他主题中发现一些有趣的信息。
标签: c scanf format-specifiers