[java] Reading Regular byte order

Started by
6 comments, last by Son of Cain 18 years, 4 months ago
I know Data Input streams and other streams That I know of read in reverse byte order but are there any that read in regular byte order? like C++ does
Advertisement
See the ByteBuffer.order(ByteOrder bo) method.
a.k.a javabeats at yahoo.ca
but will this set it so it reads the byte low byte first instead of high byte first?

public final ByteBuffer order(ByteOrder bo)

Modifies this buffer's byte order.

Parameters:
bo - The new byte order, either BIG_ENDIAN or LITTLE_ENDIAN
Returns:
This buffer


cause all i see is BIG_ENDIAN or LITTLE_ENDIAN which one of them is for regular byte order?
Neither. There's no such thing as "regular" byte order. If you mean what endian-ness your processor is, if you're on a x86 chip it's LITTLE_ENDIAN, if you're on a PowerPC it's BIG_ENDIAN.
Iam saying since you know lets say you write a program in C++ that creates a file for you. In there you store a Integer(4 Bytes) being in this order

A,B,C,D

but java reads and writes the bytes high byte first so itll turn into somethig like this

D,C,B,A

I want to know if theres a way to read it in regular byte order like how C++ wrote it.
Quote:Original post by TempHolder
Iam saying since you know lets say you write a program in C++ that creates a file for you. In there you store a Integer(4 Bytes) being in this order

A,B,C,D

but java reads and writes the bytes high byte first so itll turn into somethig like this

D,C,B,A

I want to know if theres a way to read it in regular byte order like how C++ wrote it.


Yes, there is.
www.aidanwalsh(.net)(.info)
Quote:Original post by TempHolder
Iam saying since you know lets say you write a program in C++ that creates a file for you. In there you store a Integer(4 Bytes) being in this order

A,B,C,D

but java reads and writes the bytes high byte first so itll turn into somethig like this

D,C,B,A

I want to know if theres a way to read it in regular byte order like how C++ wrote it.


There are methods you can use as well to reverse the byte order such as the following ( If I remember correctly ):

Integer.reverseBytes()
Short.reverseBytes()
Long.reverseBytes()

Floats and doubles are a little trickier to convert because their is no reverse byte method with them. What you have to do in this case is to treat the data of these types as integer data using the method Float.floatToIntBits() which gives you back the very same bits of data, but as an integer. Then reverse the bytes of this integer and treat the bits as a floating point number again using the Float.intBitsToFloat() method. A little long winded, but it does the trick..



Cliky
a.k.a javabeats at yahoo.ca

This topic is closed to new replies.

Advertisement