2015-09-09 66 views
1

我会从Ancroid客户端向Java服务器发送双数组。对于这个puropose,我开始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; 
} 
+0

只要做你在做什么反客户端,轻松。如果显示在客户端上转换你的确切代码,然后我们可能能够帮助,但它应该是相当直截了当 – musefan

回答

0

试试:

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