Java: How to convert a String of Binary values to a Float and vice-versa?

前端 未结 4 1654
情书的邮戳
情书的邮戳 2020-12-18 06:22

How do I convert the float value of 12345.12346f to a String of binary values, i.e. \"0011010101010101\", and vice-versa?

4条回答
  •  死守一世寂寞
    2020-12-18 07:05

    I'm not sure it is what you want, but here's a solution to have the binary representation of the IEEE 754 floating-point "double format" bit layout for a float (it is basically the memory representation of a float) :

    int intBits = Float.floatToIntBits(yourFloat); 
    String binary = Integer.toBinaryString(intBits);
    

    For the reverse procedure :

    int intBits = Integer.parseInt(myString, 2);
    float myFloat = Float.intBitsToFloat(intBits);
    

提交回复
热议问题