Skip to main content
gamedev.net
🔒 Locked

[java] Reading Regular byte order

Started by TempHolder Dec 4, 2005 at 9:46 AM 6 replies 500+ views
Original Post
TempHolder
TempHolder
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
TempHolder
TempHolder
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?
SiCrane
SiCrane
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.
TempHolder
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.
aidan_walsh
aidan_walsh
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)
Darragh
Darragh
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..



Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.