OK, I admit. I have lied. A little bit. I have not executed the test on a BE
platform, I have simply typed what I knew the output would be. But I got
away with it, heh heh.

But now I did it for real, on both architecture types, because I got curious
about the floats...

So:


#include <fcntl.h>

#ifndef _T
#define _T(x) x
#endif

struct endian_test_tag
{
    char    abc8[4];
    short   abc16[4];
    long    abc32[4];
    float   fpie;
} endian_struct =

{
    {'A', 'B', 'C', 0},
    {65,  66,  67,  0},
    {65,  66,  67,  0},
    1.234
};

main()
{
    int fh;
    int nbytes;
    printf(_T("sizeof(endian_struct) = %d\n"), sizeof(endian_struct));
    fh = open(_T("abc.out"), O_CREAT | O_TRUNC | O_RDWR, 0666);
    nbytes = write(fh, &endian_struct, sizeof(endian_struct));
    printf(_T("%d bytes written.\n"), nbytes);
    close(fh);
}

results in:

* on LE:
0000:0000  41 42 43 00 41 00 42 00-43 00 00 00 41 00 00 00   ABC A B C   A

0000:0010  42 00 00 00 43 00 00 00-00 00 00 00 B6 F3 9D 3F   B   C
??�?

* on BE:
0000:0000  41 42 43 00 00 41 00 42-00 43 00 00 00 00 00 41   ABC  A B C
A
0000:0010  00 00 00 42 00 00 00 43-00 00 00 00 3F 9D F3 B6      B   C
?�??


So, the floats are 'upside down' too. Makes sense. IEEE defines them in
terms of bits. And basically one can cast a float and consider it a double
word for transmission purposes. Fortunately, otherwise the whole thing would
be even a bigger mess than it already is.


Anyway, this should cover the floating point part. And thus give no excuse
to anyone to revive this thread. Unless for a better cause... ;)


Lars

Reply via email to