byte数组如何转换成double 有一个长度为8的byte数组,如何将其转化为一个double类型的数值?谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 double sum = Double.parseDouble(new String(byte[])); /** * @author infon * 2007-5-16 */public class Test { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub byte[]from=new byte[]{1,2,3,4,5,6,7,8}; StringBuffer tmp=new StringBuffer(); for(int i=0;i<from.length;i++){ tmp.append(from[i]); } double to=Double.valueOf(tmp.toString()); System.out.println(to); }} 抱歉,没说清楚,byte数组中存储的是从低位到高位排列的double类型的16进制位,谢谢了。 java.math.BigInteger bi=new java.math.BigInteger(bs); double x=bi.doubleValue(); 解决了,下面这个函数:Function for conversion of an 8 byte array to double: public static double arr2double (byte[] arr, int start) { 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); }谢谢大家了。to sunboy_yf():精度不会丢失把,都是8byte。 eclipse java文件格式化 超级硬盘数据恢复软件(superrecoery)”的注册码 一个关于java多态性的问题 util 这个单词是什么意思? 这么使用方法有问题么? 请问一个数据库连接问题!!!!!!!!!!!!!!!!!!!! Vectot的克隆问题 StyleReport中文问题 几个小问题? java对象 有没有办法设置JtabbedPane的tab(选项卡)的高度和宽度? 跪求简单java程序源码
* @author infon
* 2007-5-16
*/
public class Test { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
byte[]from=new byte[]{1,2,3,4,5,6,7,8};
StringBuffer tmp=new StringBuffer();
for(int i=0;i<from.length;i++){
tmp.append(from[i]);
}
double to=Double.valueOf(tmp.toString());
System.out.println(to);
}
}
double x=bi.doubleValue();
public static double arr2double (byte[] arr, int start) {
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);
}谢谢大家了。to sunboy_yf():
精度不会丢失把,都是8byte。