5

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

mskfisher
  • 3,291
  • 4
  • 35
  • 48
okami
  • 2,093
  • 7
  • 28
  • 40
  • do you want the representation of this float in memory ? Because float doesn't really have a binary representation, int sure have, but float not. – krtek Mar 01 '11 at 17:08
  • Do you just want any reversible conversion, or a sensible (or maybe readable) one? – Paŭlo Ebermann Mar 01 '11 at 19:59

4 Answers4

15

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);
krtek
  • 26,334
  • 5
  • 56
  • 84
  • Integer.parseLong doesn't exist – okami Mar 01 '11 at 17:38
  • thanks for the correction :) was from my first try with long instead of int – krtek Mar 01 '11 at 17:40
  • Would the result be signed or unsigned? Looking for signed. – munchschair Apr 23 '14 at 19:07
  • After hours of searching and trying I found your answer. Works as intended. Thanks alot! – Al0x Jul 10 '14 at 17:46
  • I have almost the same problem as the questioner. I have a communications simulator that genertaes Binary which is 'caught' by a listening process. I start with "float flt = -45732.9287f", and I can see that the Binary received is correct, but the conversion back to Float using this solution throws "java.lang.NumberFormatException: For input string: "11000111001100101010010011101110" from "java.lang.Integer.parseInt(Integer.java:583)"." The Binary generated is clearly a correctly structured IEEE-754 binary32 format, coming from a call to "int revint = Integer.parseInt(str, 2);" – CelticPoet Aug 05 '18 at 05:23
3

For signed Floats, use Long or BigInteger to parse the string. Casting by int causes the digit at first of 32 bits be intepreted as sign digit. procedure :

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

reverse procedure :

int intBits = new BigInteger(myString, 2).intValue();
// int intBits = (int) Long.parseLong(myString, 2);
float myFloat = Float.intBitsToFloat(intBits);
3

Working sample:

class F { 
  public static void main( String ... args ) { 
    System.out.println(
          Integer.toBinaryString( 
             Float.floatToIntBits(12345.12346f)
          ) 
     );
  }
}

Output:

1000110010000001110010001111110
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
0

You can get the bit representation of a Java Float value using

Float.floatToIntBits(float f);

Once you have done that you can test each bit of the resulting int in turn, starting with the highest, and find out if it set, writing a '1' if it is set and a '0' if not.

DJClayworth
  • 26,349
  • 9
  • 53
  • 79
  • there is a Integer.toBinaryString(intBits) method for retrieving a binary representation in String. – krtek Mar 01 '11 at 17:18