printf gives me weird extra values in some cases. What might be happening?
I'm manipulating values in an uint8_t array and printing it out to my terminal to verify if everything is correct. But for some reason I am getting weird values when I print a uint8_t - I get the expected value plus some other value (it is always the same extra value), as if I was printing a uint16_t or other 16bit value. I trimmed it all down to the following example:
This is the test code
test_payload[0] = (uint8_t)0x58;
test_payload[1] = (uint8_t)0x7B;
test_payload[2] = (uint8_t)0x6B;
test_payload[3] = (uint8_t)0x05;
test_payload[4] = (uint8_t)0x4F;
test_payload[5] = (uint8_t)0x81;
test_payload[6] = (uint8_t)0x69;
test_payload[7] = (uint8_t)0x00;
printf("%x\n\r", test_payload[0]);
printf("%x\n\r", test_payload[1]);
printf("%x\n\r", test_payload[0] + test_payload[1]);
for(i = 0; i < 8; i++)
{
printf("%x", test_payload[i]);
}
The output I get is
5803
7b03
d3
58037b0333034f03810369033
What is making it behave like that?