当前位置: 技术问答>java相关
如何把double类型值转换成字节
来源: 互联网 发布时间:2015-03-12
本文导语: 请教在Java在如何把一个double类型值转换成字节。 | private static final long LAST_OCTET_BYTE = 0x00000000000000FFl; public static final byte[] double2octet(double d) { return long2octet(Double.doubleT...
请教在Java在如何把一个double类型值转换成字节。
|
private static final long LAST_OCTET_BYTE = 0x00000000000000FFl;
public static final byte[] double2octet(double d)
{
return long2octet(Double.doubleToLongBits(d));
}
public static final byte[] long2octet(long l)
{
byte[] num = new byte[8];
num[0] = (byte)((l >>> 56) & LAST_OCTET_BYTE);
num[1] = (byte)((l >>> 48) & LAST_OCTET_BYTE);
num[2] = (byte)((l >>> 40) & LAST_OCTET_BYTE);
num[3] = (byte)((l >>> 32) & LAST_OCTET_BYTE);
num[4] = (byte)((l >>> 24) & LAST_OCTET_BYTE);
num[5] = (byte)((l >>> 16) & LAST_OCTET_BYTE);
num[6] = (byte)((l >>> 8) & LAST_OCTET_BYTE);
num[7] = (byte)((l >>> 0) & LAST_OCTET_BYTE);
return num;
}
public static final byte[] double2octet(double d)
{
return long2octet(Double.doubleToLongBits(d));
}
public static final byte[] long2octet(long l)
{
byte[] num = new byte[8];
num[0] = (byte)((l >>> 56) & LAST_OCTET_BYTE);
num[1] = (byte)((l >>> 48) & LAST_OCTET_BYTE);
num[2] = (byte)((l >>> 40) & LAST_OCTET_BYTE);
num[3] = (byte)((l >>> 32) & LAST_OCTET_BYTE);
num[4] = (byte)((l >>> 24) & LAST_OCTET_BYTE);
num[5] = (byte)((l >>> 16) & LAST_OCTET_BYTE);
num[6] = (byte)((l >>> 8) & LAST_OCTET_BYTE);
num[7] = (byte)((l >>> 0) & LAST_OCTET_BYTE);
return num;
}
|
byteValue()
Returns the value of this Double as a byte (by casting to a byte).
Returns the value of this Double as a byte (by casting to a byte).
|
可以转换,不过会损失
|
use Double.doubleToLongBits. It is the only one you should use.