typecast and bit shift

Status
Not open for further replies.

TucsonDon

Member
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?
 
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 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.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…