This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Problem on using %x for sprintf( )

Hi, Everybody,
Is there anyone have the same problems while using %x as the output type for sprintf()? The outputs have the following sequences...
0 1 2 3 4 5 6 7 8 9 @ a b c d e
But it should be...
0 1 2 3 4 5 6 7 8 9 a b c d e f

Codes:

    for(i=0;i<16;i++) {
        sprintf(Buf,"%x",(int)i);//%X has the same problem.
    }

Note: My target links c51s.lib. I have download 7.20 version.

Parents Reply Children
  • Thanks for all of your kindly reply.
    Please see the example again...

    #pragma disable
    void my_puts (void *p)
    {
        unsigned char *pb=(unsigned char *)p;
        while(*pb) {
            TI=0;
            SBUF0=*pb;
            ++pb;
            while(!TI);
        }
    }
    
    void test (void)
    {
        char xdata BufA[30];
        int x=0xABC;//2748.
        sprintf(BufA,"0xABC:%x,%X,%u:0xabc\r",x,x,x); my_puts(BufA);
        sprintf(BufA,"%d,%d,%d,%x\r",(int)(x>>8),(int)(x>>4&0xF),(int)(x&0xF),x); my_puts(BufA);
        for(;;);
    }
    

    The result from HyperTerminal are (I can see the output, so my setting/initialize should be correct) ...
    0xABC:@ab,@AB,2748:0xabc
    10,11,12,@ab

    The problem is still on %x, sprintf() seems to shift one ASCII value for hex format?

  • I think you need to post the smallest complete, compilable program that exhibits the problem.