0

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.

2
Contributors
1
Reply
15
Views
4 Years
Discussion Span
Last Post by rubberman
0

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.

Edited by rubberman

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.