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

MC51: Strange behaviour of sprintf

Ok guys, I have run into something really strange, I hope you guys have the answer for that.

How come this:

void main()
{
 U8     i;
 char aux[3];
 U8 buffer[5];

 buffer[0] = 0x0A;
 buffer[1] = 0x0B;
 buffer[2] = 0x0C;
 buffer[3] = 0x0D;
 buffer[4] = 0x0E;

 for(i=0; i<5, i++){
  sprintf(aux,"%02X",buffer[i]);
  Log_write(aux); //Prints value in new line
 }
}

Give me an output like:

A3
B3
C3
D3
E3

I'm using MC51 and S1020. I've done it step by step watching variables values in the debugger and it seems all fine but the sprintf puts those strange values in the aux variable. If I use 'i' as the argument of sprintf instead of 'buffer[i]' it does the same (the first algarism is correct and then the '3' appears, if it's a zero, an '32' appears). Even if I put a value, like '0x02' it still gives me "23" as the output.

If the value in buffer is 2 algarisms long, like 0x10, then the result is correct.
What am I doing wrong here?