How can I convert a byte array into a double and back?

Posted by user350005 on Stack Overflow See other posts from Stack Overflow or by user350005
Published on 2010-05-25T14:25:40Z Indexed on 2010/05/25 14:31 UTC
Read the original article Hit count: 178

Filed under:
|
|
|
|

For converting a byte array to a double I found this:

//convert 8 byte array to double
int start=0;//???
int i = 0;
    int len = 8;
    int cnt = 0;
    byte[] tmp = new byte[len];
    for (i = start; i < (start + len); i++) {
        tmp[cnt] = arr[i];
        //System.out.println(java.lang.Byte.toString(arr[i]) + " " + i);
        cnt++;
    }
    long accum = 0;
    i = 0;
    for ( int shiftBy = 0; shiftBy < 64; shiftBy += 8 ) {
        accum |= ( (long)( tmp[i] & 0xff ) ) << shiftBy;
        i++;
    }

        return Double.longBitsToDouble(accum);

But I could not find anything which would convert a double into a byte array.

© Stack Overflow or respective owner

Related posts about java

Related posts about conversion