Solved

how to reverse an array of bytes

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

786 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