I'm working on a project where the endianness of the data in RAM is important. If it is big endian, I need to convert it to Little Endian. Will this short program I wrote reliably determine the endianness of any Linux system?
Also, what determines the endianness of data stored in RAM?
#include <stdio.h>
#include <stdint.h>
int main ()
{
// This program will determine the endianness
// of your system and print the result in human
// readable form to stdout
uint16_t n;
uint8_t *p;
n = 0x01;
p = (uint8_t *) &n;
if (p[0]) {
printf("Little Endian\n");
} else {
printf("Big Endian\n");
}
return 0;
}