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. }
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);
Here is my test...
void puts (void *p) { unsigned char *pb=(unsigned char *)p; while(*pb) { TI=0; SBUF0=*pb; ++pb; while(!TI); } } void test (void) { char i,xdata BufA[50],BufB[50]; char xdata *p1=BufA,*p2=BufB; for(i=0;i<16;i++) { p1+=sprintf(p1,"%d,",(int)i); p2+=sprintf(p2,"%x,",(int)i); } p1=p2=0; puts(BufA); puts(BufB); }
"Am I do anything wrong?" You're overflowing BufA[] for a start.
"You're overflowing BufA[] for a start." Sorry! I thought there was a space after the %d, in your sprintf(). There isn't.
View all questions in Keil forum