开发者

return a byte array from a java method

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. A开发者_如何学JAVAlso, 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


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}));
    }

}


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.


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)


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]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜