The computer I am running the following code is Little Endian.
uint32_t src_uint = 0xAABBCCDD;
uint32_t dest_uint = 0;
uint8_t dest_arr[4] = {0};
memcpy(&dest_uint, &src_uint, 4);
printf("\ndest_uint: 0x%X\n", dest_uint);
// output: 0xAABBCCDD
memcpy(dest_arr, &src_uint, 4);
printf("\ndest_arr : 0x");
for (int i = 0; i < 4; i++)
printf("%X", dest_arr[i]);
// output: 0xDDCCBBAA
For me, the second output (0xDDCCBBAA) is correct because my computer is little endian. However, the first output (0xAABBCCDD) should be instead 0xDDCCBBAA since Little Endian would store variable src_uint
in memory as: 0xDDCCBBAA. It seems that either memcpy() is not being transparent regarding its arguments or C compiler handles int differently in those situations...
Thank you!
It's not about memcpy()
but rather how you access the destination. In the first case, you're accessing the value as a uint32_t
. In the 2nd case, you're accessing the memory one byte a time.
On big endian systems, those will give you same result. On little endian systems the results are reversed.