Solved

how to reverse an array of bytes

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.

759 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

18 Experts available now in Live!

Get 1:1 Help Now