大家好~ 我是通过如下方式转换 Float值为byte数组我接收此信息的时候该怎么从byte 转到原来的 数据呢 public byte[] valueStringToByte(String value) {

// according to the defination of protocol, convert float value to int first
// need calculate round value.
Float f = Float.parseFloat(value);
int i = Math.round(f);

byte[] bt = new byte[4];
bt[0] = (byte) (0xff & i);
bt[1] = (byte) ((0xff00 & i) >> 8);
bt[2] = (byte) ((0xff0000 & i) >> 16);
bt[3] = (byte) ((0xff000000 & i) >> 24);

return bt;
}

解决方案 »

  1.   

    你的转换方法有问题,转byte[]之前就已经把float转成int了,往回也只能转到int
      

  2.   

    public class Test2{
    public byte[] valueStringToByte(String value) { 
    float f = Float.parseFloat(value); 
    int i = Math.round(f); 
    byte[] bt = new byte[4]; 
    bt[0] = (byte) (0xff & i); 
    bt[1] = (byte) ((0xff00 & i) >> 8); 
    bt[2] = (byte) ((0xff0000 & i) >> 16); 
    bt[3] = (byte) ((0xff000000 & i) >> 24); 
    return bt; 
    }
    public int bytesToint(byte[] by){
    int x=0;
    for(int i=by.length-1;i>=0;i--){
    x<<=8;
    x|=by[i]&0xff;
    }
    return x;
    }
    public static void main(String[] args){
    Test2 test=new Test2();
    System.out.println(test.bytesToint(test.valueStringToByte("-25687.245")));
    }
    }