samtal - the value in the pastebin code is 0x100000000 which is too big to fit into an unsigned int.
By default, unsigned values are unsigned int, so the literal value should be stored as 0.
The top 1 is trimmed off. (The compiler should give a warning, but I haven't checked).
Try writing it as 0x100000000ULL
I am not yet convinced that is enough to explain everything, but it is a start.
The example program is significantly bigger than a minimal example.
It should initialise everything, so that there is no need to read input data; it is more robust to initialise than type input.
There is no example of what is printed, so it is not possible to confirm the same thing is output when run on my machine.
Here is something closer to a minimal example. It fully specifies the expected values by throwing away all of the input reading. You should initialise in_command
with the actual values used, and republish. Then we can be confident we are testing the same thing.
#include "stdlib.h"
byte op_code;
byte in_command[20] = {
0x14, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13
};
byte in_command_length;
uint64 datetime;
void setup()
{
Serial1.begin(38400);
}
void loop()
{
sort_inputs();
delay(1000);
}
void sort_inputs()
{
//Reconstruct the datetime variable by combining the 5 bytes as uint64
Serial1.println();
datetime = in_command[0] * 0x100000000; /* ERROR should be 0x100000000ULL */
Serial1.print("datetime="); Serial1.println(datetime);
datetime += in_command[1] * 0x1000000;
Serial1.print("datetime="); Serial1.println(datetime);
datetime += in_command[2] * 0x10000;
Serial1.print("datetime="); Serial1.println(datetime);
datetime += in_command[3] * 0x100;
Serial1.print("datetime="); Serial1.println(datetime);
datetime += in_command[4];
Serial1.print("datetime="); Serial1.println(datetime);
}
I've run this on my Mac, which also defaults to 32-bit ints, and the results are not negative.
It will not compile because 0x100000000 is too big to fit into an int. When that is changed to 0x100000000ULL
The output is:
datetime=85899345920
datetime=85916123136
datetime=85916254208
datetime=85916254976
datetime=85916254980
Which looks plausible.
I changed it to output in hex, and it is (still on my Mac):
datetime=1400000000
datetime=1401000000
datetime=1401020000
datetime=1401020300
datetime=1401020304
Which looks correct to me. There are only 10 hex digits, which is 40-bits, so nothing is negative.
Unless the number is printed as 64bits long (which it is not) and the MSB is 1 (which would make the number hex 8 to F), then it just means that bit is set.
Please paste your output so that we can inspect it. (Preferably print hex as that is easier to check.)
One other possibility: Serial1.println(unsigned long long)
has a bug.