Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6977103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:34:56+00:00 2026-05-27T17:34:56+00:00

Im trying to parse a bmp file with fread() and when I begin to

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:34:57+00:00Added an answer on May 27, 2026 at 5:34 pm

    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 a short value 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:

    /* CHAR_BIT == 8 assumed */
    uint16_t le16_to_cpu(const uint8_t *buf)
    {
       return ((uint16_t)buf[0]) | (((uint16_t)buf[1]) << 8);
    }
    uint16_t be16_to_cpu(const uint8_t *buf)
    {
       return ((uint16_t)buf[1]) | (((uint16_t)buf[0]) << 8);
    }
    

    You do your fread into an uint8_t buffer of the appropriate size, and then you manually copy all the data bytes over to your BMPHeader struct, converting as necessary. That would look something like this:

    /* note adjustments to type definition */
    typedef struct BMPHeader
    {
        uint8_t magic_number[2];
        uint32_t file_size;
        uint8_t reserved[4];
        uint32_t data_offset;
    } BMPHeader;
    
    /* in general this is _not_ equal to sizeof(BMPHeader) */
    #define BMP_WIRE_HDR_LEN (2 + 4 + 4 + 4)
    
    /* returns 0=success, -1=error */
    int read_bmp_header(BMPHeader *hdr, FILE *fp)
    {
        uint8_t buf[BMP_WIRE_HDR_LEN];
    
        if (fread(buf, 1, sizeof buf, fp) != sizeof buf)
            return -1;
    
        hdr->magic_number[0] = buf[0];
        hdr->magic_number[1] = buf[1];
    
        hdr->file_size = le32_to_cpu(buf+2);
    
        hdr->reserved[0] = buf[6];
        hdr->reserved[1] = buf[7];
        hdr->reserved[2] = buf[8];
        hdr->reserved[3] = buf[9];
    
        hdr->data_offset = le32_to_cpu(buf+10);
    
        return 0;
    }
    

    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 test magic_number[0]=='B' && magic_number[1]=='M'.

    Conversion in the opposite direction, btw, looks like this:

    void cpu_to_le16(uint8_t *buf, uint16_t val)
    {
       buf[0] = (val & 0x00FF);
       buf[1] = (val & 0xFF00) >> 8;
    }
    void cpu_to_be16(uint8_t *buf, uint16_t val)
    {
       buf[0] = (val & 0xFF00) >> 8;
       buf[1] = (val & 0x00FF);
    }
    

    Conversion of 32-/64-bit quantities left as an exercise.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to parse an INI file using C++. Any tips on what is
I'm trying to parse some data in a fixed format text file where each
I am trying to parse xml file and get no errors but when trying
I'm trying to write a bitmap (.bmp) parser/reader by reading raw bytes from the
I'm trying to parse a xml file and I'm having problems with a really
Whilst trying to parse MS Excel file using POI-HSSF v3.2 I am getting IndexOutOfBoundsException.
I'm trying parse an xml file and insert it into an mysql database. The
Trying to parse a csv file that has all the data wrapped in double
While trying to parse an xml file into table format, jQuery keeps closing my
Trying to parse some spam injection out of a mysql export file, and for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.