Re: big endian vs little endian data

From: A J Gage (gagey71_at_hotmail.com)
Date: 09/16/04


Date: 15 Sep 2004 22:16:43 -0700

Dave Eland <daveland@oru.edu> wrote in message news:<rhc1i053utmlo58n8ab2brg5heo5q5l6nt@4ax.com>...
> It appears that the data I/O methods for the RandomAccessFile class
> (e.g. readInt() and writeInt()) handle data in "big endian format"
> (most significant byte first). If you have a file where the data was
> written in "little endian format" (least significant byte first" how
> is the best way to read that data in Java?
>
> Dave Eland
> daveland@oru.edu

OK, if your reading ints, pullout each int and reverse the bytes.

int i = input_stream.readInt();
i = ((i & 0x000000ff) << 24) + ((i & 0x0000ff00) << 8) +
                ((i & 0x00ff0000) >>> 8) + ((i & 0xff000000) >>> 24);

Hope this helps.

AJG