I have a task to convert a 64-bit binary floating-point number into a base 10 decimal. An example here:
Input: 64-bit binary floating-point number: 0100000000001001001000011111101101010100010001000010110100011000
Output: Base 10 Decimal: 3.14
what I know here, first character is a sign = 0 (positive) , 1(negative). Next 11 bits represent the exponent, and the rests are Mantissa.
My problem is I don't know the math equation or the math solution to convert it. I am going to solve this task using Java.
CodePudding user response:
Double.longBitsToDouble(long)
takes a 64-bit long and converts it to a floating point double, which you can then print.
Given that, you can just use Long.parseLong(binary, 2)
to convert a string of 0s and 1s to a long, and then use that method to get a floating point value.
CodePudding user response:
Convert the binary into a long
value - you can use Long.parseLong(text, 2)
for that if you've got the binary value as a string... and then Double.longBitsToDouble
to perform the conversion to a double
. For example:
public class Test {
public static void main(String[] args) {
String text = "0100000000001001001000011111101101010100010001000010110100011000";
long parsedAsLong = Long.parseLong(text, 2);
double converted = Double.longBitsToDouble(parsedAsLong);
System.out.println(converted);
}
}