Print uint8_t

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)))

Orig must be a hardware Ethernet address.

+7
source share
2 answers

You need to build a suitable format string. The printf() function does not have the ability to print an array at a time, 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); 

& 0xff is to ensure onlu 8 bits are sent to printf() ; they should not be needed for an unsigned type of type uint8_t , although you can try without it.

This assumes a regular 48-bit MAC address and prints using the usual hexadecimal style.

+9
source

Use C99 format specifiers:

 #include <inttypes.h> printf("%" PRIu8 "\n", orig[0]); 
+12
source

All Articles