从java方法返回一个字节数组

时间:2011-05-26 10:31:25

标签: java string

我有一个字节数组,我将其传递给函数nibbleSwap。对于阵列中的每个字节值,nibbleSwap将第4位与第2位进行交换。交换后,我必须返回交换的字节值。如果我只是打印字节数组我能够得到正确的值但是当我返回交换的字节数组时,它不会打印正确的值。
我的代码是这样的:

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

例如。 valuebyte[]包含:91,19,38,14,47,21,11 我希望函数返回一个包含19,91,83,41,74,12,11的数组。另外,我可以通过将返回类型更改为String来将其作为String返回,因为当我这样做并打印它时,我得到了交换字节值的整数值吗?

请帮助!!
Pranay

4 个答案:

答案 0 :(得分:5)

代码完全按照您的要求执行,对您的测试数据提供当然值<91,19,38,14,47,21,11是十六进制(0x91,0x19等)。

我使用以下代码调用您的函数:

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

打印出来:

19 91 83 41 74 12 11 

要将结果作为字符串返回,您可以使用以下行中的内容:

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

}

答案 1 :(得分:0)

答案 2 :(得分:0)

  

如果我只是打印字节数组   能够获得正确的价值,但何时   我返回交换的字节数组,它   不会打印正确的值。

您的代码完全符合您的要求。

这是我的测试代码。

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

这是输出:

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)

答案 3 :(得分:0)

如果你想要的只是一个交换了半字节的字符串,我只会创建字符串,而不是创建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);
}

打印

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