float to binary or hex

tmstms
edited October 2013 in Programming Questions

hello,

I want to convert float values into binary. Of course the binary() or hex() accept integers. Before start writing a code myself based on this

http://stackoverflow.com/questions/3954498/how-to-convert-float-number-to-binary,

i would like to ask if there is an easiest way?Any suggestions?

thanks, t

Tagged:

Answers

  • Answer ✓

    http://docs.oracle.com/javase/7/docs/api/java/lang/Float.html#floatToIntBits(float)

    is the input an actual float or a string? actually, it's easy to convert from the one to the other so...

    anyway, Float object has a way of getting the internal ieee754 representation - floatToIntBits. it's static and takes a float. returns an int which you can then slice up and put into hex() or binary()

  • hm thanks for the answer. It's a little bit confusing but I will look into it more carefully. :) (the input is a float)

  • I checked the java reference,thanks

    something like this seems to works for me...or not?(it's a little confusing the whole thing with the bits)

    float x = 0.2;
    int bits = Float.floatToIntBits(x);
    
    
    println(binary(bits));
    
  • Answer ✓

    well, pi in hex should be 40490fdb according to wikipedia. which works for me

    40490fdb = 0100 0000 0100 1001 0000 1111 1101 1011

Sign In or Register to comment.