Skip to main content
Visitor II
August 18, 2010
Question

using printf to display a buffer in hex format

  • August 18, 2010
  • 5 replies
  • 5714 views
Posted on August 18, 2010 at 13:25

using printf to display a buffer in hex format

    This topic has been closed for replies.

    5 replies

    Visitor II
    May 17, 2011
    Posted on May 17, 2011 at 15:10

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

    Luca

    Visitor II
    May 17, 2011
    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

    Visitor II
    May 17, 2011
    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

    Visitor II
    May 17, 2011
    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

    Visitor II
    May 17, 2011
    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