Possible Duplicate:
Detecting endianness programmatically in a C++ program
Is there any library function available to find the endian-ness of my PC?
In case we need to deal with endianness, there are ways to detect and deal with the difference of endianness at run time using a simple program logic or utilities that are provided such as htonl, htons, ntohl, ntohs or std::endian.
We can also check the endianness of the machine using the union. We need to create a union that has an integer variable and an array of 4 characters. If the first element (au8DataBuff [0]) of the character array is equal to the LSB Bytes of integer, then the system will be little endian otherwise big-endian.
If you are not sure which endian your machine is, you can conduct this test from within IDL: little_endian = (BYTE(1, 0, 1))[0] IF (little_endian) THEN $ Print, "I'm little endian." ELSE $ Print, "I'm big endian."
Endianness is the term used to describe the byte order of data. Big-endian means data is being sent or stored with the Most Significant Byte (MSB) first. Little-endian means data is sent with the Least Significant Byte (LSB) first. A CPU always has an endianness, even for 8 bit CPUs.
Why you need a library if you can find it like this? :)
int num = 1;
if (*(char *)&num == 1)
{
printf("Little-Endian\n");
}
else
{
printf("Big-Endian\n");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With