I encountered some bizarre behavior when print-ing the result of a function that returns a char.
char readReg(char addr)
{
char result;
digitalWrite(pinCS, LOW);
SPI.transfer(addr | 0x80);
result = SPI.transfer(0x00);
digitalWrite(pinCS, HIGH);
return result;
}
void loop(){
...
Serial.println(readReg(0x00), HEX); // println does NOT handle this very well!
}
prints out "FFFFFF55" which is not correct. I would have expected either "55" or "00000055".
In order to get the correct output, I have to add:
Serial.println(0xff & readReg(0x00), HEX); // this works OK
just as a note, this does not work either:
char a = readReg(0x00);
Serial.println(a , HEX); // println does NOT handle this very well!
but this works fine:
char a = 0x55;
Serial.println(a , HEX); // works ok
but this does not:
char a = 0xff & readReg(0x00);
Serial.println(a , HEX); // total crap!
So weird!