Jump to content
  • Advertisement
Sign in to follow this  
TempHolder

[java] Reading Regular byte order

This topic is 4639 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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..



Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!