TucsonDon Member Sep 6, 2021 #1 C: uint8_t data[4]; uint32_t time; time = (uint32_t) ((data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3]); I want to shift the array into 32 byte variable, but all I get is data[0] at LSB. How do I typecast and shift?
C: uint8_t data[4]; uint32_t time; time = (uint32_t) ((data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3]); I want to shift the array into 32 byte variable, but all I get is data[0] at LSB. How do I typecast and shift?
rjenkinsgb Well-Known Member Most Helpful Member Sep 6, 2021 #2 You could put both types in a union, so the bytes in the int32 are individually accessible. Or this may work (without verifying the syntax): time = *(uint32 *) data; But be wary of byte order..
You could put both types in a union, so the bytes in the int32 are individually accessible. Or this may work (without verifying the syntax): time = *(uint32 *) data; But be wary of byte order..
danadak Well-Known Member Most Helpful Member Sep 6, 2021 #3 You were left shifting a byte variable multiple bytes wider that its type before you cast it into a 32 bit variable. Cast the byte variable then do the shift and OR ? Not a C expert here, still a novice..... Regards, Dana.
You were left shifting a byte variable multiple bytes wider that its type before you cast it into a 32 bit variable. Cast the byte variable then do the shift and OR ? Not a C expert here, still a novice..... Regards, Dana.
TucsonDon Member Sep 6, 2021 #4 rjenkinsgb time = *(uint32 *) data worked I just had to change the way that the data is loaded into the array (in reverse)
rjenkinsgb time = *(uint32 *) data worked I just had to change the way that the data is loaded into the array (in reverse)