【问题标题】:How can I convert a byte array into a double and back?如何将字节数组转换为双精度并返回?
【发布时间】:2011-02-23 17:39:03
【问题描述】:

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

//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);

但我找不到任何可以将双精度转换为字节数组的东西。

【问题讨论】:

  • 你想要多少字节?例如,从技术上讲,可以制作一个字节数组,每个字节只包含一位信息。
  • Pops:我认为暗示 Octavian 只需要存储双精度表示的完整表示所需的字节数是公平的,即匹配内部双精度表示。在 Java 中,这可以用 (int)(Double.Size/Byte.Size) 来计算。

标签: java double bytearray


【解决方案1】:
long bits = Double.doubleToLongBits(myDouble);

【讨论】:

  • 我看了你的问题 - 这不是一个完整的答案 - 但是,如果你真的想要一个字节 [],可以反向使用与上述相同的方法。
【解决方案2】:
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),
    };
}

【讨论】:

  • 这是一个不完整的答案——你会发现问题出在另一半;重构双精度不仅仅是推动 (int)b[0]
  • 也许是这样,但在 Java ME 中是不可能的。这是一个可行的解决方案。谢谢。 +1
【解决方案3】:

该功能已在 API 中实现。将字节数组包装在ByteBuffer 中并使用ByteBuffer.putLong 和ByteBuffer.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]

【讨论】:

  • 将此与Double.longBitsToDouble 和反相结合,答案是完整的(glowcoder 的答案)。
  • 我不明白,为什么在往返 Double 和 Byte 时要通过位?
  • 你可以用buf.asLongBuffer().put(longArray);将整个数组炸到缓冲区中
【解决方案4】:

甚至更简单,

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

【讨论】:

  • 很高兴知道这与手写转换的性能相比如何。
  • return ByteBuffer.wrap(new byte[8]).putDouble(value).array() 将第一种方法变成另一种方法
  • @cubiclettuce 遗憾的是,array() 不需要返回任何内容,因为支持结构不需要是 byte[]。也就是说,它可能会按此顺序抛出UnsupportedOperationException 或ReadOnlyBufferException。
  • ByteBuffer.wrap(new byte[8]) 可以解决问题。来自 ByteBuffer.wrap 的 Java 文档:“新缓冲区将由给定的字节数组支持”
【解决方案5】:

我实际上遇到了双精度数的上限和下限问题,这似乎是我见过的唯一可以纠正该问题的代码。我希望它能帮助其他人在这个领域寻找答案。如果您确实使用其他代码,请确保您测试了所有值范围,您应该编写一个循环来转换所有值并断言它们以确保它们。

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

【讨论】:

    【解决方案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();
    }
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2018-12-30
      • 1970-01-01
      • 2013-03-10
      • 1970-01-01
      • 2014-11-02
      • 2010-11-21
      • 2015-01-13
      • 2012-03-14
      • 1970-01-01
      相关资源
      最近更新 更多