Sign in to follow this  

[java] Reading Regular byte order

This topic is 4389 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

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

This topic is 4389 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this