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

时间:2015-09-09 15:43:26

标签: java android base64

我会从Ancroid客户端向Java服务器发送一个双数组。对于这个目的,我开始在客户端将int转换为字节数组,然后使用Base64

对其进行编码

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

我在客户端使用此方法

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

1 个答案:

答案 0 :(得分:1)

试一试:

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;
    }
相关问题