Hey,
Iv been looking around for a simple way (meaning less than 10 lines) to reverse the order of my bytes in a byte array. At the moment my code is:

     public byte[] outputarray()
    {
      byte b[] = new byte[10];  
      ByteBuffer buf = ByteBuffer.wrap(b);
      buf.putLong(output); 
    }

The problem is that the array is on the wrong order. So when i save the byte array to the file its contents in binary are in reverse. So anyone know the easiest way or possibly an api way to easily reverse the bytes, i tried flip but it doesent seem to be what im looking for.
The oracle website claims to have a reverse function in ByteBuffer but it does not show up in eclipse:
http://download.oracle.com/otn_hosted_doc/jdeveloper/1012/jdev-doc/oracle/ide/util/ByteBuffer.html

Any help would be nice :)

Recommended Answers

All 4 Replies

Is it the byte array you want to reverse before doing the wrap?

yes, it makes no difference when so long as the content contained in the array is in the opposite order. So element [10] becomes element [1] basically. But id prefer to do it with an api such as .reverse() function rather than having to use a for loop.

You can continue reading the API doc for a method or write a simple loop.
Writing the loop will probably be the shorter and quicker way.

Thanks, looked through most. my only other option was to reverse the order of the long's bits prior to the function entirely. But i already created the for-loop, just seems a waste of code for something...so simple. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.