1

I have a byte array which i am passing to a function nibbleSwap. nibbleSwap swaps the 1st 4 bits with the 2nd 4 bits for each byte value in the array. After swapping i have to return the swapped byte values. If i just print the byte array i am able to get the correct value but when i return the swapped byte array, it does not print the correct value.
My code is like this:

private static byte[] nibbleSwap(byte []inByte){
        int []nibble0 = new int[inByte.length];
        int []nibble1 = new int[inByte.length];
        byte []b = new byte[inByte.length];

        for(int i=0;i<inByte.length;i++)
        {
                nibble0[i] = (inByte[i] << 4) & 0xf0;
                 nibble1[i] = (inByte[i] >>> 4) & 0x0f;
                b[i] =(byte) ((nibble0[i] | nibble1[i]));
                /*System.out.printf(" swa%x ",b[i]); ---   if i do this by changing the return to void i get the correct output.
        */
                   }

        return b;
    }  

eg. valuebyte[] contains: 91,19,38,14,47,21,11 I want the function to return an array containing 19,91,83,41,74,12,11. Also, can i return this as a String by changing the return type as String because when i did that and printed it, i got the integer values of the swapped byte values?

Please Help!!
Pranay

4
  • I see no reason why this would fail when returning the bytes. My guess is that it's in how you're checking the returned values. Please show a short but complete program which demonstrates the problem. It's not at all obvious why you've got the nibble arrays, btw. Commented May 26, 2011 at 10:36
  • 1
    Could you add the code snippet calling nibbleSwap and with the way you print the nibble swapped array? Commented May 26, 2011 at 10:38
  • byte []something = nibbleSwap(valuebyte); for(int i=0;i<valuebyte.length;i++) System.out.printf(" %x ",something[i]) when i use %x, i get the correct output , but i want that i can print the array using println also in which case the output printed is garbage. Commented May 26, 2011 at 10:42
  • So you have valueByte[0]=91 and you get something[0]=181 while you want to have something[0]=19. Am I correct? Commented May 26, 2011 at 10:48

4 Answers 4

5

The code does exactly what you say you want it to do, on your test data, provided of course that the values (91, 19, 38, 14, 47, 21, 11) are hexadecimal (0x91, 0x19, and so on).

I used the following code to call your function:

public static void main(String[] args) {
    byte[] swapped = nibbleSwap(new byte[]{(byte)0x91, 0x19, 0x38, 0x14, 0x47, 0x21, 0x11});
    for (byte b : swapped) {
        System.out.printf("%x ", b);
    }
    System.out.println();
}

This prints out:

19 91 83 41 74 12 11 

To return the result as a string, you could use something along the following lines:

public class NibbleSwap {

    private static String nibbleSwap(byte []inByte){
        String ret = "";
        for(int i = 0; i < inByte.length; i++)
        {
                int nibble0 = (inByte[i] << 4) & 0xf0;
                int nibble1 = (inByte[i] >>> 4) & 0x0f;
                byte b = (byte)((nibble0 | nibble1));
                ret += String.format("%x ", b);
        }

        return ret;
    }  

    public static void main(String[] args) {
        System.out.println(nibbleSwap(new byte[]{(byte)0x91,0x19,0x38,0x14,0x47,0x21,0x11}));
    }

}
Sign up to request clarification or add additional context in comments.

2 Comments

What If i want the swapped array to be returned as String? I do not want to use printf.
then have a method of a similar name but indicating that the result is String change its return type to String, still have the same arguments. In its body first call nibbleSwap as the first line get its result then build your String and return it. :) Just like @aix did in the edit +1.
0

I do not get your problem clearly but when it goes to its second part as to printing the result array, you do not need to modify the method you just need to do something like in the code below on the result array you get from the method.

byte[] b = new byte[]{1,2,3,4};
System.out.println(Arrays.toString(b));

Output:

[1, 2, 3, 4]

I hope this answers (at least part of) your problem.

Comments

0

If i just print the byte array i am able to get the correct value but when i return the swapped byte array, it does not print the correct value.

Your code does exactly what you want.

Here is my test code.

public class StackOverflow {

    public static void main(String args[]) throws NullPointerException {

        byte[] original = "Kerem".getBytes();

        System.out.print("\n--------Print From Original--------------\n");
        for (int i = 0; i < original.length; i++) {
            System.out.printf("%x ", original[i]);
        }
        System.out.print("\n--------Print From Method Body-----------\n");
        byte[] returnValue = nibbleSwap(original);
        System.out.print("\n--------Print From Method Return Value---\n");
        for (int i = 0; i < returnValue.length; i++) {
            System.out.printf("%x ", returnValue[i]);
        }
    }

    private static byte[] nibbleSwap(byte[] inByte) {
        int[] nibble0 = new int[inByte.length];
        int[] nibble1 = new int[inByte.length];
        byte[] b = new byte[inByte.length];

        for (int i = 0; i < inByte.length; i++) {
            nibble0[i] = (inByte[i] << 4) & 0xf0;
            nibble1[i] = (inByte[i] >>> 4) & 0x0f;
            b[i] = (byte) ((nibble0[i] | nibble1[i]));
            System.out.printf("%x ", b[i]);
        }
        return b;
    }
}

Here is the output:

run:
--------Print From Original--------------
4b 65 72 65 6d 
--------Print From Method Body-----------
b4 56 27 56 d6 
--------Print From Method Return Value---
b4 56 27 56 d6 
-----------------------------------------
BUILD SUCCESSFUL (total time: 0 seconds)

Comments

0

If all you want is a String with the nibbles swapped, I would just create the String as you go and not create byte[]s.

public static String nibbleSwapAsString(byte[] inByte) {
    StringBuilder sb = new StringBuilder("[");
    String sep = "";
    for (byte b : inByte) {
        sb.append(sep).append(Integer.toHexString(b & 0xF)).append(Integer.toHexString(b >> 4 & 0xF));
        sep = ", ";
    }
    sb.append(']');
    return sb.toString();
}

public static void main(String... args) {
    String text = nibbleSwapAsString(new byte[]{(byte)0x91, 0x19, 0x38, 0x14, 0x47, 0x21, 0x11});
    System.out.println(text);
}

prints

[19, 91, 83, 41, 74, 12, 11]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.