Endian change & Byte Array -> Struct
So, I recently ran into a problem where I have a byte array { 0, 196, 0, 0, 0, 42 } and I need to copy that into a struct defined as:
Code:
typedef struct tdFOO {
UINT16 ID;
UINT32 RetVal;
.
.
.
} FOO;
Also, the byte order in the byte array needs to be reversed. I'm communicating with a system with a different endianness, so I can't just memcpy the byte array into my structure. Also, when I did try (before I realized the bytes were out of order), the memcpy only populated the first structure variable (ID) and none of the others.
What I actually need is for the byte array to look like: { 196, 0, 42, 0, 0, 0 }
Which when copied into the structure would cause ID = 196 and RetVal to = 42
The only way I can think to do it is to write a function that swaps the appropriate number of bytes based on the type of the struct variable where it will be assigned, and then do a memcpy of the bytes I just swapped directly into the struct variable.
Then I just do that over, and over, and over again until I've copied every value.
Thanks!