double类型如何转化为字节数组?

解决方案 »

  1.   

    也许这是你想要的。~
    public class  bd {    public static void main(String[] args){
            byte[] b={10,20,30};
            long[] a = new long[b.length];
            double[] d=new double[b.length];
           for (int i = 0; i < b.length; i++) {
               a[i] = (long) b[i];
               d[i] = Double.longBitsToDouble(a[i]);
               System.out.println(d[i]);
           }
        }
    }
       我也是个初学者 不知道对不对喔~
      

  2.   

    根据楼上的改改了改,不晓得对不对哦 public static void main(String[] args) {
            byte[]   b={10,20,30}; 
            long[]   a   =   new   long[b.length]; 
            double[]   d=new   double[b.length]; 
            d[0]=12.0;d[1]=13.0;
          for   (int   i   =   0;   i   <   b.length;   i++)   { 
                  a[i]   =   (long)  d[i];
                  b[i]=Byte.parseByte(String.valueOf(a[i]));
                  System.out.println(b[i]); 
          }  }
      

  3.   

    不明白楼主的意思,double占8个字节,难道是要转成IEEE754 64位双精度浮点数的计算机内部表示?
      

  4.   

    public class DoubleTest {
        
        public static void main(String[] args) {
            double numPI = Math.PI;        
            byte[] bytes = double2bytes(numPI);        
            // 输出
            for(int i = 0; i < bytes.length; i++) {
                System.out.printf("%4d", bytes[i]);
            }
        }
        
        public static byte[] double2bytes(double num) {
            long nums = Double.doubleToLongBits(num);
            byte[] bytes = new byte[Double.SIZE / Byte.SIZE];        
            // 0为高位字节,7为低位字节
            for(int i = 0; i < bytes.length; i++) {
                bytes[bytes.length - i - 1] = (byte)((nums >> (i * 8)) & 0xff);
            }
            return bytes;
        }
    }
      

  5.   


              double d = 3.101;
              String s = String.valueof(d);
              byte[] b = s.getBytes();
      

  6.   


    ByteArrayOutputStream baos=new ByteArrayOutputStream();
    DataOutputStream dos=new DataOutputStream(baos);
    dos.writeDouble(0.5);//写入double值
    byte[] b=baos.toByteArray();