Solved

how to reverse an array of bytes

Posted on 2004-08-16
3
228 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 300 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java 1603 Error 3 50
Understanding websocket example in spring 1 57
Java Server Faces parameter pass? 6 39
Java SE 8u111  Lot of stuff broke 11 54
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now