-
Struct Casting
Hi All
I'm trying to get uint32_t data into a 4 * uint8_t struct, I dont want to pass in byte by byte so i am trying to achieve soemthing like the following:
Code:
uint32_t test = 0xFFFFFFFF;
typedef struct word32To4xByte
{
uint8_t byte1;
uint8_t byte2;
uint8_t byte3;
uint8_t byte4;
}wordtobyte;
struct word32To4xByte expectedResp;
ecpectedResp= (wordtobyte) test;
-
You could do something like this.
Code:
uint32_t test = 0xFFFFFFFF;
typedef struct {
uint8_t byte1;
uint8_t byte2;
uint8_t byte3;
uint8_t byte4;
} wordtobyte;
wordtobyte expectedResp;
expectedResp = *((wordtobyte*)(&test));
Or use memcpy(). :)
-
Load in bytes, read dword... load in dword, read bytes...
Code:
typedef union t_dwordtobyte
{
uint32_t dword;
struct bytes
{
uint8_t b1;
uint8_t b2;
uint8_t b3;
uint8_t b4;
}
}dwordtobyte;
// use as...
dwordtobyte wb;
wb.dword = utf32_value;
byte1 = wb.bytes.b1;
Note... this will expose you to Endian issues, so the order of bytes in the example may not be correct for all cases.