Code decoding byte

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 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

Did you clear the 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) );

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) );

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 4976 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.