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?
EDIT:
If you simply wish to copy a 16 bit number into an array of bytes, the correct solution is this:
memcpy(buffer, &value, sizeof(uint16_t)).
We cannot do *(uint16_t*) buffer=value;
because it invokes undefined behavior. buffer
could be misaligned and it's also a strict aliasing violation. And this is why I wrote with emphasis above "this is fine for character types only".