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
  • I tried the following in V7.20.

    #include <stdio.h>
    #include <string.h>
    
    xdata char buf [100] = "";
    xdata char buf2 [100] = "";
    
    void main (void)
    {
    unsigned char i;
    
    for (i = 0; i < 16; i++)
      {
      sprintf (buf, "%x", (int) i);
      strcat(buf2,buf);
      strcat(buf2," ");
      }
    
    while(1);

    After running this example, buf2 contained "0 1 2 3 4 5 6 7 8 9 a b c d e f " as I expected. Maybe something is corrupting your output?

    Jon

Reply
  • I tried the following in V7.20.

    #include <stdio.h>
    #include <string.h>
    
    xdata char buf [100] = "";
    xdata char buf2 [100] = "";
    
    void main (void)
    {
    unsigned char i;
    
    for (i = 0; i < 16; i++)
      {
      sprintf (buf, "%x", (int) i);
      strcat(buf2,buf);
      strcat(buf2," ");
      }
    
    while(1);

    After running this example, buf2 contained "0 1 2 3 4 5 6 7 8 9 a b c d e f " as I expected. Maybe something is corrupting your output?

    Jon

Children
More questions in this forum