cancel
Showing results for 
Search instead for 
Did you mean: 

using printf to display a buffer in hex format

lowpowermcu
Associate II
Posted on August 18, 2010 at 13:25

using printf to display a buffer in hex format

5 REPLIES 5
luca239955_stm1_st
Senior II
Posted on May 17, 2011 at 15:10

I think replacing'' %04X'' with ''%2x'' should work (haven't tried though)

Luca

lowpowermcu
Associate II
Posted on May 17, 2011 at 15:10

Hi ubiali,

I have tried ''%2x'' and ''%1X'' and I got 800 for 0x08 and 1f00 for 0x1f.

What could be the solution ? Is related to stdlib ?

Thanks for the help !

Thanks & Regards,

MCU Lüfter

luca239955_stm1_st
Senior II
Posted on May 17, 2011 at 15:10

Hi,

I just tried

unsigned int buffer[] = {0x08, 0x1f};

void main(void)

{

printf(''%02x %02x'', buffer[0], buffer[1]);

}

and it prints

08 1f

with the Cosmic stm8 compiler.

Regards,

Luca

luca239955_stm1_st
Senior II
Posted on May 17, 2011 at 15:10

cast to int when calling printf:

printf(''%02x %02x'', (int)buffer[0], (int)buffer[1]);

Regards,

Luca

lowpowermcu
Associate II
Posted on May 17, 2011 at 15:10

Hi ubiali,

Thanks for your test but my buffer is unit8_t type and when I print I am getting ''800'' and ''1f00''.

I think that ''%02x'' works for unsigned int (16 bits) and not for uint8_t (8 bits).

What could you suggest ?

By curiosity, I have tried another compiler and I have found that it prints with %2.2 (and I am not sure that this is what should be).

I am really thankful.

Regards,

MCU Lüfter