0

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 :)

2
Contributors
4
Replies
5
Views
4 Years
Discussion Span
Last Post by trishtren
0

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.

1

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.

0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.