Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how to reverse an array of bytes

Posted on 2004-08-16
3
Medium Priority
?
243 Views
Last Modified: 2010-03-31
Is there any simple way of reversing an array of bytes?Any examples would be nice
Thanks In advance
0
Comment
Question by:inzaghi
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
TrekkyLeaper earned 1200 total points
ID: 11812462
public void reverse( byte[] b ) {
  for( int i = 0; i < b.length / 2; i++ ) {
    swap( b, i, b.length - i - 1 );
  }
}

private void swap( byte[] array, int a, int b ) {
  int temp = array[a];
  array[a] = array[b];
  array[b] = temp;
}
0
 
LVL 35

Expert Comment

by:girionis
ID: 11812478
If "bytes" is an array of bytes

for (int i=0; i<bytes.length; i++)
{
    bytes[i] = bytes[i] == 0 ? 1 : 0;
}
0
 
LVL 35

Expert Comment

by:girionis
ID: 11812492
bah.. ignore my comment... I was thinking bits instead :(
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question