?
Solved

swap byte order from an array

Posted on 2011-05-11
7
Medium Priority
?
575 Views
Last Modified: 2012-05-11
Hi there,

I have a byte array,

for example, 0x1 0x2 0x3 0x4 0x5 0x6

I want to change this array into another array where the contents are swap on 16bit boundary)
so that the new array becomes 0x2 0x1, 0x4, 0x3, 0x6, 0x5,..
0
Comment
Question by:ambuli
  • 3
  • 3
7 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 35738362
/**
   * Byte swap a single int value.
   * 
   * @param value  Value to byte swap.
   * @return       Byte swapped representation.
   */
  public static int swap (int value)
  {
    int b1 = (value >>  0) & 0xff;
    int b2 = (value >>  8) & 0xff;
    int b3 = (value >> 16) & 0xff;
    int b4 = (value >> 24) & 0xff;

    return b1 << 24 | b2 << 16 | b3 << 8 | b4 << 0;
  }

Open in new window

0
 
LVL 23

Expert Comment

by:cmalakar
ID: 35738367

byte arr[] = {0x1, 0x2, 0x3, 0x4};

for(int i = 0; (i+1) < arr.length; i = i + 2){
    arr[i] = arr[i+1];
}

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35738395
Yes, soorry my posting was doing different thing - misuderstood the question
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 23

Expert Comment

by:cmalakar
ID: 35738396
It should swap ;-)
byte arr[] = {0x1, 0x2, 0x3, 0x4};

for(int i = 0; (i+1) < arr.length; i = i + 2){
    byte temp = arr[i];
    arr[i] = arr[i+1];
    arr[i+1] = temp;
}

Open in new window

0
 
LVL 23

Accepted Solution

by:
cmalakar earned 1600 total points
ID: 35738427
>> I want to change this array into another array
byte arr[] = {0x1, 0x2, 0x3, 0x4};
byte newArr[] = new byte[arr.length];
for(int i = 0; (i+1) < arr.length; i = i + 2){
    newArr[i] = arr[i+1];
    newArr[i+1] = arr[i];
}

Open in new window

0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 400 total points
ID: 35738431
I thought you need to do this:
byte arr[] = {0x1, 0x2, 0x3, 0x4};

for(int i = 0; (i+1) < arr.length; i = i + 2){
   byte temp = arr[i];
    arr[i] = arr[i+1];
   arr[i+1]=temp;
}

Open in new window

0
 

Author Comment

by:ambuli
ID: 35738631
Thank you all.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn different types of Android Layout and some basics of an Android App.
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 …
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…
Suggested Courses
Course of the Month9 days, 23 hours left to enroll

569 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