本文介绍了如何将字节数组转换为双精度并返回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为了将字节数组转换为双精度数,我发现了这一点:
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.
推荐答案
或者更简单,
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();
}
这篇关于如何将字节数组转换为双精度并返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!