Boost logo

Boost :

From: Marco Manfredini (marco_at_[hidden])
Date: 2000-03-10 16:37:09

Heeee. You stole it from me :-)

No, honestly, the trick works very good for me! It is very useful to read
file formats in different or mixed endian orders (such strange stuff
exists!), you only need to declare a structure with the endian'ed ints and
you read the data completly transparent. Even from a memory-mapped file:

// Such strange looks an ESRI ArcView shapefile header...
struct shp_file_header
        big_long file_code; // == 9944
        big_long unused_a[5];
        big_long file_length; // Anzahl der 16-Bit worte.
        lit_long version; // ==1000
        lit_long shape_type; //
        geo_rect<lit_double> bounds;
        lit_long unused_b[8];

In my code, I swapped the endians with unions, I think it makes it easier to
see, what actually happens:

static inline unsigned long endswap (unsigned long v)
        union { unsigned long x; unsigned char b[4]; } s,d;

        d.b[0] = s.b[3];
        d.b[1] = s.b[2];
        d.b[2] = s.b[1];
        d.b[3] = s.b[0];
        return d.x;

One point: If I'm not utterly wrong, then IEEE's (though hard to believe)
have a big and a little endian representation. Does someone knows more?

- Marco

Boost list run by bdawes at, gregod at, cpdaniel at, john at