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
  • Here are a few notes.

    1. I compiled your example. The compiler complained about redefining puts (so I called my copy my_puts). This may be causing problems.

    2. What I compiled and tested worked just fine.

    3. You didn't include a complete working example (it compiled with errors and the serial port is not configured). So, I can't really debug those things for you. I added those things in my test.

    4. Your treatment of the TI flag in the puts function is not ideal. You should really check if TI is set at the beginning of the loop rather than at the end. Checking at the end consumes time unnecessarily. That time could be spent executing other code.

    5. You don't specify a type for BufA and BufB. The compiler assumes int. You probably meant char.

    6. p1=p2=0; I assume this is supposed to null-terminate the strings. It doesn't. For that you'll need *p1='\0'; *p2='\0';

    7. Overall, for what it does, the code snippet you have uses a lot of variables. If you are just learning C, this is OK. But, they are not required.

    Following is my example that works OK.

    #include <reg51.h>
    #include <stdio.h>
    
    
    void my_puts (char *p)
    {
    for (; *p; p++)
      {
      while (TI == 0);
      SBUF = *p;
      TI = 0;
      }
    }
    
    xdata char BufA[50];
    xdata char BufB[50];
    
    
    void main (void)
    {
    char i;
    char xdata *p1=BufA,*p2=BufB;
    
    SCON  = 0x50;		        /* SCON: mode 1, 8-bit UART, enable rcvr      */
    TMOD |= 0x20;               /* TMOD: timer 1, mode 2, 8-bit reload        */
    TH1   = 221;                /* TH1:  reload value for 1200 baud @ 16MHz   */
    TR1   = 1;                  /* TR1:  timer 1 run                          */
    TI    = 1;                  /* TI:   set TI to send first char of UART    */
    
    for(i = 0; i < 16; i++)
      {
      p1 += sprintf(p1, "%d,", (int)i);
      p2 += sprintf(p2, "%x,", (int)i);
      }
    
    *p1='\0';
    *p2='\0';
    
    my_puts(BufA);
    my_puts(BufB);
    
    while (1);
    }
    

    Jon

Reply
  • Here are a few notes.

    1. I compiled your example. The compiler complained about redefining puts (so I called my copy my_puts). This may be causing problems.

    2. What I compiled and tested worked just fine.

    3. You didn't include a complete working example (it compiled with errors and the serial port is not configured). So, I can't really debug those things for you. I added those things in my test.

    4. Your treatment of the TI flag in the puts function is not ideal. You should really check if TI is set at the beginning of the loop rather than at the end. Checking at the end consumes time unnecessarily. That time could be spent executing other code.

    5. You don't specify a type for BufA and BufB. The compiler assumes int. You probably meant char.

    6. p1=p2=0; I assume this is supposed to null-terminate the strings. It doesn't. For that you'll need *p1='\0'; *p2='\0';

    7. Overall, for what it does, the code snippet you have uses a lot of variables. If you are just learning C, this is OK. But, they are not required.

    Following is my example that works OK.

    #include <reg51.h>
    #include <stdio.h>
    
    
    void my_puts (char *p)
    {
    for (; *p; p++)
      {
      while (TI == 0);
      SBUF = *p;
      TI = 0;
      }
    }
    
    xdata char BufA[50];
    xdata char BufB[50];
    
    
    void main (void)
    {
    char i;
    char xdata *p1=BufA,*p2=BufB;
    
    SCON  = 0x50;		        /* SCON: mode 1, 8-bit UART, enable rcvr      */
    TMOD |= 0x20;               /* TMOD: timer 1, mode 2, 8-bit reload        */
    TH1   = 221;                /* TH1:  reload value for 1200 baud @ 16MHz   */
    TR1   = 1;                  /* TR1:  timer 1 run                          */
    TI    = 1;                  /* TI:   set TI to send first char of UART    */
    
    for(i = 0; i < 16; i++)
      {
      p1 += sprintf(p1, "%d,", (int)i);
      p2 += sprintf(p2, "%x,", (int)i);
      }
    
    *p1='\0';
    *p2='\0';
    
    my_puts(BufA);
    my_puts(BufB);
    
    while (1);
    }
    

    Jon

Children