I am trying to parse the FAT filesystem & I notice that many of the fields are more than 2 bytes. I was reading someone's program and he has defined unsigned short for a given field of 2 bytes. Now, my question is how does the compiler handle endianness? If I convert the short into hex by using 02x, the data is handled properly. The file is opened in binary mode & I wonder how data more than a byte can be interpreted i.e. how does the compiler know which byte is the LSB/MSB? The same goes for long and int.

AFAIK, FAT file systems are all encoded in the format used by Intel x86 processors - LittleEndian. Use this to test:

#include <endian.h>
#if __BYTE_ORDER == __LITTLE_ENDIAN
/* We have a LittleEndian machine - Intel most likely, but possibly an ARM or MIPS in LE configuration. */
#elif __BYTE_ORDER == __BIG_ENDIAN
/* We have a BigEndian machine - an old 68000 system perhaps? */
#else
/* We have something else - probably an old PDP-11 */
#endif

This construct is commonly used in system-level programming.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.