buffer.push_back((BYTE)(time_utc & 0x000000FF));
buffer.push_back((BYTE)((time_utc & 0x0000FF00) >> 8));
buffer.push_back((BYTE)( (time_utc & 0x00FF0000) >> 16));
buffer.push_back((BYTE)((time_utc & 0xFF000000) >> 24));
On the server, I receive bytes And store them in socket_buf, start from index 0-3 and decode them using the following logic
mypkt.dateTime = ((socket_buf[0] << 24) +
(socket_buf[1] << 16) + socket_buf[2] << 8) +
(socket_buf[3] << 0));
But I’m not sure Is it correct to decode it because the date I get is incorrect. Can anyone suggest me the correct way to decode it? I use Linux commands to decode the date (16711840 is the number I obtained through decoding):
#date -d @16711840
Are you sure your computer is big-endian?
Also, let me give you a suggestion: use the OR operator instead of the plus sign.
This can save you more time.
mypkt.dateTime = (long) ((socket_buf[0] << 24) | (socket_buf[1] << 16) | socket_buf[2] << 8) | (socket_buf[3] << 0) );
I have an embedded device that sends UTC date in this format (date is 4 bytes):
buffer.push_back((BYTE)(time_utc & 0x000000FF));
buffer.push_back((BYTE)((time_utc & 0x0000FF00) >> 8));
buffer. push_back((BYTE)((time_utc & 0x00FF0000) >> 16));
buffer.push_back((BYTE)((time_utc & 0xFF000000) >> 24));
On the server , I receive the bytes and store them in socket_buf, start from index 0-3 and decode them using the following logic
mypkt.dateTime = ((socket_buf[ 0] << 24) +
(socket_buf[1] << 16) + socket_buf[2] << 8) +
(socket_buf[3] << 0));
< p> But I am not sure if I decode it correctly, because the date I get is incorrect. Can anyone suggest me the correct way to decode it? I use Linux commands to decode the date (16711840 is the number I obtained through decoding):
#date -d @16711840
#date -d @16711840
p>
Did you clear socket_buf?
Are you sure your computer is big-endian?
Also, let me give you a suggestion: use the OR operator instead of the plus sign.
This can save you more time.
mypkt.dateTime = (long) ((socket_buf[0] << 24) | (socket_buf[1] << 16) | socket_buf[2] << 8) | (socket_buf[3] << 0) );