munchschair
munchschair

Reputation: 1683

Signed equivalent of Integer.toBinaryString method?

I am using Integer.toBinaryString to convert floats to intbits (Float.floatToIntBits()) into binary. However, the result is unsigned. Is there a way I can get a signed base two result? I am not looking for a two's compliment representation.

I also want to do the reverse, turned a signed binary number to float.

Upvotes: 0

Views: 304

Answers (1)

Dawood ibn Kareem
Dawood ibn Kareem

Reputation: 79875

If value is the int that you want to convert, you could do something like

value < 0 ? 
    ( "-" + Integer.valueOf(-value).toBinaryString()) :
    Integer.valueOf(value).toBinaryString();

I don't want to answer your second question here - you added it after I posted this.

Upvotes: 1

Related Questions