Big versus little endian (byte order)

G

Gido

My questions is: How can I read little endian values from a file stream?

I am trying to read values from a binary file in the WINDOWS platform.
The file includes values of type integer (signed 32-bit integer - 4 bytes) and
double (signed 64-bit IEEE double-precision floating point numbers).
I am using the readInt() and readDouble() functions as defined in the
java.io.* package. However, the values are stored in big and little byte order.
The readInt() and readDouble() functions correctly return only the big endian
values.

My questions is: How can I read the little endian values?
 
X

xarax

My questions is: How can I read little endian values from a file stream?

I am trying to read values from a binary file in the WINDOWS platform.
The file includes values of type integer (signed 32-bit integer - 4 bytes) and
double (signed 64-bit IEEE double-precision floating point numbers).
I am using the readInt() and readDouble() functions as defined in the
java.io.* package. However, the values are stored in big and little byte order.
The readInt() and readDouble() functions correctly return only the big endian
values.

My questions is: How can I read the little endian values?

The java.nio (new I/O) package that is new with jdk1.4 has ByteBuffer
and friends that support big and little endian. Use the new I/O
facilities.
 
G

Gido

Jon

Thanks a lot. Your information was exactly the jump start I needed.
From here on it will be easy.

Thanks
Gido


Jon A. Cruz said:
Gido said:
My questions is: How can I read little endian values from a file stream?

Either use someone else's class (like Roedy's LEDataStream
http://mindprod.com/jgloss/ledatastream.html), or just do it yourself.

Read 4 bytes. Shift and mask. Voila! You're done. It should end up
looking just like the C/C++ code should.


byte tmp[4] = {0,0,0,0};
in.readFully( tmp );
int val = ((0x0ff & tmp[0]) << 0)
| ((0x0ff & tmp[1]) << 8)
| ((0x0ff & tmp[2]) <<16)
| ((0x0ff & tmp[3]) <<24);



And to read a Double

Read 8 bytes.
Pack them back into a long. Just like that int code but the values keep
going.

Then

Double dbl = Double.longBitsToDouble( val );
 
G

Gido

Roedy

Your web site is a gold mine! Thanks so much for your generous contributions.

Thanks
Gido
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top