how to convert binary float to decimal float?

Recommended Answers

What is binary float?

Jump to Post

You don't actually have to go to all that trouble, though...

int val = Integer.parseInt(str, 2);
float f = Float.intBitsToFloat(val);
Jump to Post

All 8 Replies

actually I mean binary(IEEE754 format) representation of real number.

base http://java.sun.com/docs/books/jls/

Float f = -Float.MIN_VALUE;
        Integer i = Float.floatToIntBits(f);

        System.out.println(Integer.toBinaryString(i));
        System.out.println(Integer.toHexString(i));

        Integer j = Float.floatToRawIntBits(f);

        System.out.println(Integer.toBinaryString(i));
        System.out.println(Integer.toHexString(i));
        
        //10000000000000000000000000000001

also explore javadoc,sourcecode of ==>java.lang.Float ,java.lang.Double
funny number, isn't it?

base http://java.sun.com/docs/books/jls/

Float f = -Float.MIN_VALUE;
        Integer i = Float.floatToIntBits(f);

        System.out.println(Integer.toBinaryString(i));
        System.out.println(Integer.toHexString(i));

        Integer j = Float.floatToRawIntBits(f);

        System.out.println(Integer.toBinaryString(i));
        System.out.println(Integer.toHexString(i));
        
        //10000000000000000000000000000001

also explore javadoc,sourcecode of ==>java.lang.Float ,java.lang.Double
funny number, isn't it?

I have IEEE754 representation of real number 2.5 as follows:
01000000001000000000000000000000

How can I retrieve 2.5 back from this representation?

IEEE754 Float Bits:

0 10000000 01000000000000000000000
Divide to 3 parts (sign, exponent, fraction) .Parse them separately. PICTURE http://en.wikipedia.org/wiki/IEEE_754-1985
Once more read all your posts on the same theme and write at end your code.
(even start point of your code)

You don't actually have to go to all that trouble, though...

int val = Integer.parseInt(str, 2);
float f = Float.intBitsToFloat(val);
commented: It worked! Thanks!! +1

step by step
You have a String of length =32 on input
You need 3 substrings as result.

public class StringSplitter {
//0 10000000 01000000000000000000000
//Divide to 3 parts (sign, exponent, fraction) .Parse them separately

    private String s32; // source string
    // parts of string
    private String sign;
    private String exponent;
    private String fraction;

    public StringSplitter(String s32) {
        this.s32 = s32;
        split();
    }

    private void split() {
        //TODO
        //0         10000000      01000000000000000000000
        //sign=     exponent=         fraction=
        // xxx =  s32.substring(beginIndex, endIndex)
    }

    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("source string= ");
        sb.append(s32);
        sb.append(" sign= ");
        sb.append(sign);
        sb.append(" exponent= ");
        sb.append(exponent);
        sb.append(" fraction= ");
        sb.append(fraction);
        return sb.toString();
    }

    public static void main(String[] args) {
        String inputString = "01000000001000000000000000000000";
        StringSplitter ss = new StringSplitter(inputString);
        System.out.println(ss);
    }
}

Fill the split() method using String-class methods.

You don't actually have to go to all that trouble, though...

int val = Integer.parseInt(str, 2);
float f = Float.intBitsToFloat(val);

Thank you very much!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.