If I run the following code the result, newnum, comes out right if the second byte is less than 0x80. If i run the code, replacing the 7f with 80 or greater the result is low by 0x1000. I do not understand. I thought that it had to do with the long being signed, but tried it with unsigned long and the result was the same. I figure the 0x1000 difference is a clue, but to what?
output with byte 2 less than 0x80
AABB7FDD
DD 7F BB AA
AABB7FDD
output with byte 2 0x80 or greater
AABB80DD
DD 80 BB AA
AABA80DD
long num = 0xaabb7fdd; 0xaabb80dd has error
byte byt[5];
void setup()
{
Serial.begin(9600);
Serial.println(num, HEX);
Serial.println(num, BIN);
byt[0] = num;
byt[1] = num>>8;
byt[2] = num>>16;
byt[3] = num>>24;
Serial.print(byt[0], HEX);
Serial.print(" ");
Serial.print(byt[1], HEX);
Serial.print(" ");
Serial.print(byt[2], HEX);
Serial.print(" ");
Serial.println(byt[3], HEX);
byt[4] = '\0';
long newnum = (byt[3] * 16777216) + (byt[2] * 65536) + (byt[1] * 256) + byt[0];
Serial.println(newnum, HEX);
Serial.println(newnum, BIN);
}
void loop()
{
}