I found this code on SO.
unsigned char * SerializeInt(unsigned char *buffer, int value)
{
/* Write big-endian int value into buffer; assumes 32-bit int and 8-bit char. */
buffer[0] = value >> 24;
buffer[1] = value >> 16;
buffer[2] = value >> 8;
buffer[3] = value;
return buffer + 4;
}
You can see the code claims it is writing an integer into the buffer, in a Big Endian Way.
My question is: Does this function work correctly on both Little endian and Big endian machines? In other words, on both LE and BE machines, does it store the number to buffer in a Big Endian Way? And if yes, Why?