如何将字节数组转换为double和back?

Oct*_*ian 44 java double bytearray

为了将字节数组转换为double,我发现了这个:

//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);
Run Code Online (Sandbox Code Playgroud)

但我找不到任何可以将double转换为字节数组的东西.

use*_*4ce 108

甚至更简单,

import java.nio.ByteBuffer;

public static byte[] toByteArray(double value) {
    byte[] bytes = new byte[8];
    ByteBuffer.wrap(bytes).putDouble(value);
    return bytes;
}

public static double toDouble(byte[] bytes) {
    return ByteBuffer.wrap(bytes).getDouble();
}
Run Code Online (Sandbox Code Playgroud)

  • 很高兴知道这与手写的性能转换相比如何. (3认同)

cor*_*iKa 13

long bits = Double.doubleToLongBits(myDouble);
Run Code Online (Sandbox Code Playgroud)

  • 我看了你的问题 - 这不是一个完整的答案 - 但是,如果你真的想要一个byte [],可以反过来使用与上面相同的方法. (2认同)

JRL*_*JRL 10

public static byte[] toByteArray(double d) {
    long l = Double.doubleToRawLongBits(d);
    return new byte[] {
        (byte)((l >> 56) & 0xff),
        (byte)((l >> 48) & 0xff),
        (byte)((l >> 40) & 0xff),
        (byte)((l >> 32) & 0xff),
        (byte)((l >> 24) & 0xff),
        (byte)((l >> 16) & 0xff),
        (byte)((l >> 8) & 0xff),
        (byte)((l >> 0) & 0xff),
    };
}
Run Code Online (Sandbox Code Playgroud)

  • 这是一个不完整的答案 - 你会发现问题出在另一半; 重构双重不仅仅是推(int)b [0] << 56 |的问题 (int)b [1] << 48 | (int)b [2] << 40(...)到Double.longBitsToDouble().ByteBuffer解决方案更优雅,更令人头疼. (2认同)
  • 也许是这样,但Java ME不可能.这是一个有效的解决方案.谢谢.+1 (2认同)

aio*_*obe 10

该功能已在API中实现.将字节数组包装在一个ByteBuffer并使用ByteBuffer.putLongByteBuffer.getLong:

import java.nio.*;
import java.util.Arrays;

public class Test {
    public static void main(String... args) throws Exception {

        long[] longArray = { 1234, 2345, 3456 };

        // Longs to bytes
        byte[] bytes = new byte[longArray.length * 8];
        ByteBuffer buf = ByteBuffer.wrap(bytes);
        for (long l : longArray)
            buf.putLong(l);

        System.out.println(Arrays.toString(bytes));

        // Bytes to longs
        ByteBuffer buf2 = ByteBuffer.wrap(bytes);
        long[] longs = new long[bytes.length / 8];
        for (int i = 0; i < longs.length; i++)
            longs[i] = buf2.getLong(i*8);

        System.out.println(Arrays.toString(longs));

    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

[0, 0, 0, 0, 0, 0, 4, -46, 0, 0, 0, 0, 0, 0, 9, 41, 0, 0, 0, 0, 0, 0, 13, -128]
[1234, 2345, 3456]
Run Code Online (Sandbox Code Playgroud)


Nil*_*ora 6

public static final short byteArrayToShort(byte[] bytes) {
    return ByteBuffer.wrap(bytes).getShort();
}

public static final int byteArrayToInt(byte[] bytes) {
    return ByteBuffer.wrap(bytes).getInt();
}

public static final float byteArrayToFloat(byte[] bytes) {
    return ByteBuffer.wrap(bytes).getFloat();
}

public static double byteArrayToDouble(byte[] bytes) {
    return ByteBuffer.wrap(bytes).getDouble();
}

public static final long byteArrayToLong(byte[] bytes) {
    return ByteBuffer.wrap(bytes).getLong();
}
Run Code Online (Sandbox Code Playgroud)

去享受吧。