I'm trying to convert back from a byte buffer to it corresponding ulong, but can't recreate the value... I must be blind or doing something wrong here :(
ulong us = (ulong)(millisecondsSinceUnixEpoch * 1000.0);
// xxxxxxxx-xxxx-0000-0000-000000000000
_buffer[0] = (byte)(us >> 40);
_buffer[1] = (byte)(us >> 32);
_buffer[2] = (byte)(us >> 24);
_buffer[3] = (byte)(us >> 16);
_buffer[4] = (byte)(us >> 8);
_buffer[5] = (byte)us;
ulong reUs = ((ulong)_buffer[0] << 40) |
((ulong)_buffer[1] << 32) |
((ulong)_buffer[2] << 24) |
((ulong)_buffer[3] << 16) |
((ulong)_buffer[4] << 8) |
_buffer[5];
Debug.WriteLine(us);
Debug.WriteLine(reUs);
Results in:
1691334871851000 (us)
2485011587064 (reUs)
Expecting that (reUs == us)
As replies told, I'm losing a byte making number to not match. That is due my wrong calculation to turn seconds into milliseconds which is not necessary since the value already came in ms unit! The correct code is: