Im trying to parse a bmp file with fread() and when I begin to parse, it reverses the order of my bytes.
typedef struct{
short magic_number;
int file_size;
short reserved_bytes[2];
int data_offset;
}BMPHeader;
...
BMPHeader header;
...
The hex data is 42 4D 36 00 03 00 00 00 00 00 36 00 00 00;
I am loading the hex data into the struct by fread(&header,14,1,fileIn);
My problem is where the magic number should be 0x424d //'BM' fread() it flips the bytes to be 0x4d42 // 'MB'
Why does fread() do this and how can I fix it;
EDIT: If I wasn’t specific enough, I need to read the whole chunk of hex data into the struct not just the magic number. I only picked the magic number as an example.
This is not the fault of
fread, but of your CPU, which is (apparently) little-endian. That is, your CPU treats the first byte in ashortvalue as the low 8 bits, rather than (as you seem to have expected) the high 8 bits.Whenever you read a binary file format, you must explicitly convert from the file format’s endianness to the CPU’s native endianness. You do that with functions like these:
You do your
freadinto anuint8_tbuffer of the appropriate size, and then you manually copy all the data bytes over to yourBMPHeaderstruct, converting as necessary. That would look something like this:You do not assume that the CPU’s endianness is the same as the file format’s even if you know for a fact that right now they are the same; you write the conversions anyway, so that in the future your code will work without modification on a CPU with the opposite endianness.
You can make life easier for yourself by using the fixed-width
<stdint.h>types, by using unsigned types unless being able to represent negative numbers is absolutely required, and by not using integers when character arrays will do. I’ve done all these things in the above example. You can see that you need not bother endian-converting the magic number, because the only thing you need to do with it is testmagic_number[0]=='B' && magic_number[1]=='M'.Conversion in the opposite direction, btw, looks like this:
Conversion of 32-/64-bit quantities left as an exercise.