I have an external ADC that delivers three bytes of 2s complement data for a 24-bit signed integer result. I'm having problems with Sketch properly converting the numbers:
union sfp24bit {
byte b[2];
signed long int result;
} tval;
byte incomingdata[2];
......
// convert three bytes of 2s complement into 24 bit signed integer
tval.b[2] = incomingdata[2]; // high byte
tval.b[1] = incomingdata[1]; // middle byte
tval.b[0] = incomingdata[0]; // low byte
tval.result /= 2;
Any ideas on getting the conversion to operate properly in Sketch?
byte incomingdata[3];
long val = 0;
......
// convert three bytes of 2s complement into 24 bit signed integer
if (incomingdata[2] & 0x80)
val = 0xff;
val = (val << 8) | incomingdata[2];
val = (val << 8) | incomingdata[1];
val = (val << 8) | incomingdata[0];
// val is now a 32-bit signed integer