Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将数组字节小端数转换为大端数?_Java - Fatal编程技术网

Java 如何将数组字节小端数转换为大端数?

Java 如何将数组字节小端数转换为大端数?,java,Java,你有办法通过这个单元测试吗 @Test public void convert() { byte[] num = {1,0,0,0};//little endian byte[] answer = {0,0,0,1};//big endian assertArrayEquals(answer , Convert.converting(num)); } public static byte[] converting(byte[] value) {

你有办法通过这个单元测试吗

@Test public void convert() {   
    byte[] num = {1,0,0,0};//little endian  
    byte[] answer = {0,0,0,1};//big endian  
    assertArrayEquals(answer , Convert.converting(num)); 
}

public static byte[] converting(byte[] value) {
    //TO DO     
    /* ByteBuffer bb = ByteBuffer.wrap(nombre); // Not working
    bb.order( ByteOrder.LITTLE_ENDIAN);
    return bb; */
}

使用
ByteBuffer.order(ByteOrder.LITTLE_ENDIAN)
时,不会更改ByteBuffer的“内部”表示形式。它只允许您以正确的尾数表示形式从中获取整数/long等(因为您需要具有多字节类型才能具有有意义的完整尾数)。

您可以通过将转换方法更改为以下方式,自己反转
字节[]
数组:

public static byte[] converting(byte[] value) {
    final int length = value.length;
    byte[] res = new byte[length];
    for(int i = 0; i < length; i++) {
        res[length - i - 1] = value[i];
    }
    return res;
}
公共静态字节[]转换(字节[]值){
最终整数长度=value.length;
字节[]res=新字节[长度];
for(int i=0;i
您是否认为
ByteBuffer
可以隐式转换为
字节[]
?您能给出更多示例输入和所需输出吗?我的意思是如果你只是想通过测试,你可以
返回新的字节[]{0,0,0,1} AsOrdByTeSe()/<代码>方法或类似的方法,它将根据当前设置的字节顺序进行交换。如果这个问题被证明是有用的,你可以考虑接受其中一个答案。