给你一个把byte[] 转化成int的代码,转化成float也一样。
public static int byteArrayToInt(byte[] byteArrayData){
        return  (byteArrayData[0] & 0xff) << 24 |
                (byteArrayData[1] & 0xff) << 16 |
                (byteArrayData[2] & 0xff) << 8  |
                (byteArrayData[3] & 0xff);
    }

解决方案 »

  1.   

    to woowindice(黑山老妖) 
    可是float的存储格式,与int似乎是不同的?
    还有就是,java和c 在处理浮点数时处理方式是否一致?
      

  2.   

    to woowindice(黑山老妖) 
    float不支持位运算。to  vive(白起) 
    我是由二进制文件中读取数据,无法使用字符串
      

  3.   


    好像 Float 类有一个静态的方法专门用于处理这类问题的静态方法 intBitsToFloat()试一下这样行不行:public static int byteArrayToFloat(byte[] byteArrayData){return Float.intBitsToFloat(
                    (byteArrayData[0] & 0xff) << 24 |
                    (byteArrayData[1] & 0xff) << 16 |
                    (byteArrayData[2] & 0xff) << 8  |
                    (byteArrayData[3] & 0xff) ) ;
    }
      

  4.   

    将byte数组转化为int整数后,调用Float.intBitsToFloat()生成的float为什么有的正确,有的错误?
    还有,intBitsToFloat()的算法小弟看了之后觉得很怪,解释不通?哪未能帮忙解释一下
    再有就是,在 c中float在内存中是如何组织的?四个字节分别存储什么东西?
      

  5.   

    public static int floatToIntBits(float value) Returns the bit represention of a single-float value. 
    The result is a representation of the floating-point argument according to the IEEE 754 floating-point "single precision" bit layout.
    Bit 31 (the bit that is selected by the mask 0x80000000) represents the sign of the floating-point number. 
    Bits 30-23 (the bits that are selected by the mask 0x7f800000) represent the exponent. 
    Bits 22-0 (the bits that are selected by the mask 0x007fffff) represent the significand (sometimes called the mantissa) of the floating-point number. If the argument is positive infinity, the result is 0x7f800000. 
    If the argument is negative infinity, the result is 0xff800000. 
    If the argument is NaN, the result is 0x7fc00000. In all cases, the result is an integer that, when given to the intBitsToFloat(int) method, will produce a floating-point value equal to the argument to floatToIntBits不用再翻译了吧
      

  6.   

    首先这个应该是与平台相关的, 因为涉及到 Little Endian 和
    Big Endian 的问题.Float 和 Double 的文档中有明确的格式说明.