繁体   English   中英

如何将字节数组转换为双精度数组

[英]How to convert a byte array to double array

我会从Ancroid客户端向Java服务器发送一个双精度数组。 为此,我开始在客户端将int转换为字节数组,然后使用Base64对其进行编码

现在,我想知道如何执行反向操作,例如将接收到的字节array []转换回double数组[]

我在客户端使用了这种方法

    public byte[] toByteArray(double[] from) {
    byte[] output = new byte[from.length*Double.SIZE/8]; 
    int step = Double.SIZE/8;
    int index = 0;
    for(double d : from){
        for(int i=0 ; i<step ; i++){
            long bits = Double.doubleToLongBits(d); 
            byte b = (byte)((bits>>>(i*8)) & 0xFF);

            int currentIndex = i+(index*8);
            output[currentIndex] = b;
        }
        index++;
    }
    return output;
}

试试吧 :

public static double[] toDoubleArray(byte[] byteArray){
        int times = Double.SIZE / Byte.SIZE;
        double[] doubles = new double[byteArray.length / times];
        for(int i=0;i<doubles.length;i++){
            doubles[i] = ByteBuffer.wrap(byteArray, i*times, times).getDouble();
        }
        return doubles;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM