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

时间:2010-05-25 14:25:40

标签: 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);

但我找不到任何可以将double转换为字节数组的内容。

6 个答案:

答案 0 :(得分:98)

甚至更简单,

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

答案 1 :(得分:13)

long bits = Double.doubleToLongBits(myDouble);

答案 2 :(得分: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),
    };
}

答案 3 :(得分:9)

该功能已在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));

    }
}

输出:

[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]

答案 4 :(得分:4)

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

去享受。

答案 5 :(得分:2)

我实际上遇到了double的上下两个问题,这似乎是我见过的唯一能够纠正的代码。我希望它可以帮助其他人在这个领域寻找答案。如果你去寻找其他代码,确保你测试了所有的值,你应该编写一个循环来转换为所有值并从中断开它们以确定它们。

// byte2Double method - extracts doubles from byte array
// source: http://www.java2s.com/Code/Java/Data-Type/bytetoDouble.htm
  public static final double[] byte2Double(byte[] inData, boolean byteSwap) {
    int j = 0, upper, lower;
    int length = inData.length / 8;
    double[] outData = new double[length];
    if (!byteSwap)
      for (int i = 0; i < length; i++) {
        j = i * 8;
        upper = (((inData[j] & 0xff) << 24)
            + ((inData[j + 1] & 0xff) << 16)
            + ((inData[j + 2] & 0xff) << 8) + ((inData[j + 3] & 0xff) << 0));
        lower = (((inData[j + 4] & 0xff) << 24)
            + ((inData[j + 5] & 0xff) << 16)
            + ((inData[j + 6] & 0xff) << 8) + ((inData[j + 7] & 0xff) << 0));
        outData[i] = Double.longBitsToDouble((((long) upper) << 32)
            + (lower & 0xffffffffl));
      }
    else
      for (int i = 0; i < length; i++) {
        j = i * 8;
        upper = (((inData[j + 7] & 0xff) << 24)
            + ((inData[j + 6] & 0xff) << 16)
            + ((inData[j + 5] & 0xff) << 8) + ((inData[j + 4] & 0xff) << 0));
        lower = (((inData[j + 3] & 0xff) << 24)
            + ((inData[j + 2] & 0xff) << 16)
            + ((inData[j + 1] & 0xff) << 8) + ((inData[j] & 0xff) << 0));
        outData[i] = Double.longBitsToDouble((((long) upper) << 32)
            + (lower & 0xffffffffl));
      }

    return outData;
  }