I have variable uint16 value, I want to copy it to uint8_t buffer[3]. Is it possible to do (Little endian):
*buffer=*(uint8_t *)&value;
Instead of:
buffer[0] = highByte(value);
buffer[1] = lowByte(value);
Since this replacement cause stm32f7 I2C to not working correctly. Is there any correct casting?
>Solution :
STM32 is little endian so you get the lowest significant byte first:
uint8_t* ptr = (uint8_t*)&value;
uint8_t low = ptr[0];
uint8_t high = ptr[1];
Doing casts and de-referencing like this is fine for character types only. The above code is assuming that uint8_t is a character type, which is very likely the case (on gcc and other mainstream compilers).
For more info see What is CPU endianness?