Have you tried to access individual bytes?
uint8_t packet[4]; uint32_t value; packet[0] = (value >> 24) & 0xFF; packet[1] = (value >> 16) & 0xFF; packet[2] = (value >> 8) & 0xFF; packet[3] = value & 0xFF;
Probably more efficient than calling a function. :-)
Note: the above method is platform independent. It does not require knowledge of how value is stored.
Explanation:
Let packet be the buffer or memory location for uint32_t , which must be saved in the buffer in Big Endian format (the most significant byte).
The expression ( value >> 24 ) shifts the most significant byte to the least significant (byte) position. The expression "& 0xff" truncates, discards, any extraneous values, which leads to an 8-bit unsigned value. Then the value is stored in the most significant position in the buffer in the first place.
Similarly for the remaining bytes.
Thomas Matthews
source share