JAVA的类型转换..........在线等待 小弟最近需要实现double型值和byte[8]数组相互无缝转换(double转换成长度为8位的byte数组,8位的byte数组转换为double),不知道有哪位大侠能指点一二.小弟不胜感激 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 最好能否例子,如double m_d_double=(double)0.13;byte[] m_byte=new byte[8];相互转换 另外double存储布局是8字节所以简单的可以这样来做对于double d;转换byte[] b = new byte[8];for (int i = 0; i < 8; i ++) b[i] = (byte)((d >> i*8)&0x000000FF);return b; 对于字节byte[] b;转换double d;for (int i = 7; i >= 0 ; i --){ d = b[i]|0x00FF; d << 8;}return b; 晕,double怎么移位?Double.longBitsToDoubleDouble.dobuleToLongBits double d ;System.out.println(d);byte[] bs = new byte[8];for (int i=0;i<8;i++){ Double db = new Double(d % 256); bs[i] = db.byteValue(); d = d / 256;}java double移位不好用, 不知道有没有效率更高的 能否再详细一点,首先确认一点,double是无法移位的.另外,Double.longBitsToDouble,Double.dobuleToLongBits能够实现无疑转换吗? byte[]->double byte[] bytes = new byte[]{(byte)0x12, (byte)0x0F, (byte)0xF0}; java.math.BigInteger bi = new java.math.BigInteger (bytes); double result = bi.doubleValue(); double ->byte[]: 首先判断输入的double值有没有小数点,如果没有: Double input = new Double(128); java.math.BigInteger bi1 = new java.math.BigInteger (String.valueOf(input.intValue())); bytes = bi1.toByteArray(); 如果有小数点,按照小数点分成2部分,分别用以上方法转换,分别写入0-3,4-7byte数组中 byte[]->double Double.longBitsToDouble(makeLong(b7, b6, b5, b4, b3, b2, b1, b0) public static long makeLong(byte b7, byte b6, byte b5, byte b4, byte b3, byte b2, byte b1, byte b0) { return ((((long) b7 & 0xff) << 56) | (((long) b6 & 0xff) << 48) | (((long) b5 & 0xff) << 40) | (((long) b4 & 0xff) << 32) | (((long) b3 & 0xff) << 24) | (((long) b2 & 0xff) << 16) | (((long) b1 & 0xff) << 8) | (((long) b0 & 0xff) << 0)); }double ->byte[]double xlong longX = Double.doubleToRawLongBits(x)byte b7= long7(longX);....byte b0 = long0(longX); private static byte long7(long x) { return (byte) (x >> 56); } private static byte long6(long x) { return (byte) (x >> 48); } private static byte long5(long x) { return (byte) (x >> 40); } private static byte long4(long x) { return (byte) (x >> 32); } private static byte long3(long x) { return (byte) (x >> 24); } private static byte long2(long x) { return (byte) (x >> 16); } private static byte long1(long x) { return (byte) (x >> 8); } private static byte long0(long x) { return (byte) (x >> 0); } 分两步,先转换成long再利用Long.longBitsToDouble()方法 1.利用函数生成long public static long byte2long(byte[] b,int offset) { return ((long)b[offset+7]&0xff) | (((long)b[offset+6]&0xff)<<8) | (((long)b[offset+5]&0xff)<<16) | (((long)b[offset+4]&0xff)<<24) | (((long)b[offset+3]&0xff)<<32) | (((long)b[offset+2]&0xff)<<40) | (((long)b[offset+1]&0xff)<<48) | ((long)b[offset]<<56); }2.利用函数Long.longBitsToDouble()转化成double一定可以的 继续HttpClient研究,Sina博客发帖 多线程问题,生产者和消费者问题 debug bug 如果得到TextArea中的内容再传值 怎么能把java写的代码编译成exe或其他安装程序,让它可以独立运行? 如何利用设置classpath引用同一个包中的另一个实用类? 用Eclipse debug时, Scanner总是出错 载入不了图像,拜托进来看看,我郁闷死了 帮帮我,快疯了! startserver 出现endpoint created: :8080就没有反应,请帮忙? JBuilder与WebLogic的最高版本是什么及正版要多少钱? 请教实现示波器功能的绘图实现
double m_d_double=(double)0.13;
byte[] m_byte=new byte[8];
相互转换
对于double d;转换
byte[] b = new byte[8];
for (int i = 0; i < 8; i ++)
b[i] = (byte)((d >> i*8)&0x000000FF);return b;
double d;
for (int i = 7; i >= 0 ; i --)
{
d = b[i]|0x00FF;
d << 8;
}return b;
Double.longBitsToDouble
Double.dobuleToLongBits
System.out.println(d);
byte[] bs = new byte[8];
for (int i=0;i<8;i++){
Double db = new Double(d % 256);
bs[i] = db.byteValue();
d = d / 256;
}
java double移位不好用, 不知道有没有效率更高的
byte[] bytes = new byte[]{(byte)0x12, (byte)0x0F, (byte)0xF0};
java.math.BigInteger bi = new java.math.BigInteger (bytes);
double result = bi.doubleValue(); double ->byte[]:
首先判断输入的double值有没有小数点,如果没有:
Double input = new Double(128);
java.math.BigInteger bi1 = new java.math.BigInteger (String.valueOf(input.intValue()));
bytes = bi1.toByteArray();
如果有小数点,按照小数点分成2部分,分别用以上方法转换,分别写入0-3,4-7byte数组中
Double.longBitsToDouble(makeLong(b7, b6, b5, b4, b3, b2, b1, b0)
public static long makeLong(byte b7,
byte b6,
byte b5,
byte b4,
byte b3,
byte b2,
byte b1,
byte b0) {
return ((((long) b7 & 0xff) << 56) | (((long) b6 & 0xff) << 48)
| (((long) b5 & 0xff) << 40) | (((long) b4 & 0xff) << 32)
| (((long) b3 & 0xff) << 24) | (((long) b2 & 0xff) << 16)
| (((long) b1 & 0xff) << 8) | (((long) b0 & 0xff) << 0));
}
double ->byte[]
double xlong longX = Double.doubleToRawLongBits(x)
byte b7= long7(longX);
....
byte b0 = long0(longX);
private static byte long7(long x) {
return (byte) (x >> 56);
} private static byte long6(long x) {
return (byte) (x >> 48);
} private static byte long5(long x) {
return (byte) (x >> 40);
} private static byte long4(long x) {
return (byte) (x >> 32);
} private static byte long3(long x) {
return (byte) (x >> 24);
} private static byte long2(long x) {
return (byte) (x >> 16);
} private static byte long1(long x) {
return (byte) (x >> 8);
} private static byte long0(long x) {
return (byte) (x >> 0);
}
再利用Long.longBitsToDouble()方法
public static long byte2long(byte[] b,int offset)
{
return ((long)b[offset+7]&0xff) | (((long)b[offset+6]&0xff)<<8) |
(((long)b[offset+5]&0xff)<<16) | (((long)b[offset+4]&0xff)<<24) |
(((long)b[offset+3]&0xff)<<32) | (((long)b[offset+2]&0xff)<<40) |
(((long)b[offset+1]&0xff)<<48) | ((long)b[offset]<<56);
}
2.利用函数Long.longBitsToDouble()转化成double
一定可以的