1

I would sent a double array from Ancroid client to Java server. For this puropose, I starts to convert int to byte array on client side then encode it with Base64

Now , I want to know how to perform the reverse operation , e.g. converting back the received byte array[]to double array []

I used this method on the client side

    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
  • Just do the reverse of what you do at the client end, easy. If you show your exact code that is converting on the client then we may be able to assist, but it should be fairly straight forward Commented Sep 9, 2015 at 15:45

1 Answer 1

1

Try it :

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;
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.