Java 如何将字节数组转换为双精度并返回?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2905556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I convert a byte array into a double and back?
提问by Octavian
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.
但是我找不到任何可以将双精度转换为字节数组的东西。
回答by corsiKa
long bits = Double.doubleToLongBits(myDouble);
回答by JRL
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),
};
}
回答by aioobe
The functionality is implemented in the API already. Wrap the byte array in a ByteBuffer
and use ByteBuffer.putLong
and ByteBuffer.getLong
:
该功能已在 API 中实现。将字节数组包装在 a 中ByteBuffer
并使用ByteBuffer.putLong
and 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));
}
}
Output:
输出:
[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]
回答by usethe4ce
Or even simpler,
或者更简单的,
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();
}
回答by hamish
I actually ran into problems with upper and lower of the double, this seems to be the only code I have seen that corrects for that. I hope it assists others searching for answers in this area. if you do go for some other code, make sure you test the full range of values, you should write a loop that converts to and from for all values and assert them to be sure.
我实际上遇到了 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;
}
回答by Nilesh Vora
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();
}
Go and Enjoy.
去享受吧。