打印uint8_t [英] printing the uint8_t
问题描述
我有 uint8_t orig [ETH_ALEN];
如何使用 __ printf(3,4)
其定义为 #define __printf(a,b)__attribute __((格式(printf,a,b)))
Orig应该是以太网硬件地址。
您需要构造一个合适的格式字符串。 printf()
函数无法一次性打印数组,因此您需要将其拆分并打印每个 uint8_t
:
__ printf( MAC:%02x:%02x:%02x:%02x:%02x:%02x\ n,
orig [0]& 0xff,orig [1]& 0xff,orig [2]& 0xff,
orig [3]& 0xff,orig [4]& 0xff,orig [5]& 0xff);
& 0xff
是为了确保仅将8位发送到 printf()
;但是,对于 uint8_t
这样的无符号类型,则不需要使用它们,因此您也可以尝试不使用它们。
常规的48位MAC,并使用常规用冒号分隔的十六进制样式进行打印。 / p>
I have uint8_t orig[ETH_ALEN];
How can I print it using __printf(3, 4)
which is defined as #define __printf(a, b) __attribute__((format(printf, a, b)))
the Orig should be ethernet hardware address.
You need to construct a format string that's suitable. The printf()
function has no way of printing an array in one go, so you need to split it and print each uint8_t
:
__printf("MAC: %02x:%02x:%02x:%02x:%02x:%02x\n",
orig[0] & 0xff, orig[1] & 0xff, orig[2] & 0xff,
orig[3] & 0xff, orig[4] & 0xff, orig[5] & 0xff);
The & 0xff
is to ensure onlu 8 bits is sent to printf()
; they shouldn't be needed for an unsigned type like uint8_t
though so you can try without too.
This assumes a regular 48-bit MAC, and prints using the conventional colon-separated hex style.
这篇关于打印uint8_t的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!