Connecting Tech Pros Worldwide Help | Site Map

big endian vs little endian data

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 11:33 PM
Dave Eland
Guest
 
Posts: n/a
Default big endian vs little endian data

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

  #2  
Old July 17th, 2005, 11:46 PM
A J Gage
Guest
 
Posts: n/a
Default Re: big endian vs little endian data

Dave Eland <daveland@oru.edu> wrote in message news:<rhc1i053utmlo58n8ab2brg5heo5q5l6nt@4ax.com>. ..[color=blue]
> 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[/color]

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
  #3  
Old July 17th, 2005, 11:47 PM
A. W. Dunstan
Guest
 
Posts: n/a
Default Re: big endian vs little endian data

Dave Eland wrote:
[color=blue]
> 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[/color]

Look at ByteBuffer & company. Something like this:

RandomAccessFile file = new RandomAccessFile(filename, "r");
byte[] recordBuffer = new byte[RECORD_LENGTH];
ByteBuffer record = ByteBuffer.wrap(recordBuffer);
record.order(ByteOrder.BIG_ENDIAN);
FloatBuffer floatRecordBuffer = record.asFloatBuffer();
IntBuffer intRecordBuffer = record.asIntBuffer();

Use

file.seek(offset);
file.read(recordBuffer);

to read the file, and

intRecordBuffer.rewind();
intRecordBuffer.get(arrayOfInts, offset, sizeOfArray);

to get it into your arrayOfInts.



 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.