On Thu, 2010-10-28 at 13:18 -0600, AJ ONeal wrote:
> I'm trying to read a bmp header, but the bytes seem to be coming in in
> the wrong order (endianness).

You're incorrectly assuming that structs are packed as tightly as
possible. In fact, they're padded. I've included code below to
demonstrate this.

http://en.wikipedia.org/wiki/Data_structure_alignment#Typical_alignment_of_C_structs_on_x86

If you don't care about anything other than gcc, it's possible to pack
structs.

http://axelio.wordpress.com/2007/07/24/be-careful-with-packed-structures/

> How can I get all of the values I'm reading in from the header in the
> file in reverse order?

For correctness, you should be converting from little endian to host
byte order. On x86 this is a noop, but still a good habit.

man endian

===
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct
{
    uint16_t type; // uint8_t magic[2]
    uint32_t size;
    uint16_t reserved1;
    uint16_t reserved2;
    uint32_t offset;
} Header;

int main(int argc, char *argv[]) {
    Header h = {0};
    printf("%p - %p = %d\n", &(h.size), &(h.type),
                             (void *)&(h.size) - (void *)&(h.type));
}
===

--------------------
BYU Unix Users Group 
http://uug.byu.edu/ 

The opinions expressed in this message are the responsibility of their
author.  They are not endorsed by BYU, the BYU CS Department or BYU-UUG. 
___________________________________________________________________
List Info (unsubscribe here): http://uug.byu.edu/mailman/listinfo/uug-list

Reply via email to