Solved

# hex / binary to floating point representation

Posted on 2003-03-18
Medium Priority
556 Views
hi, i saw a post by surball that asked how to convert a hex value into a floating point representation.

the example given was:  0x3f800000 should yield 1.0

one of the suggested answers (by heyhey) was as follows:

long binData = 0x3f800000;
double d = Double.longBitsToDouble(binData);

however, when i print out the value for "d", i get 5.263544247E-315.
question 1:  why is this answer different from the expected "1.0"?
question 2: i assume the binary representation of 0x3f800000 is: 0011 1111 1000 0000 0000 0000 0000 0000.  is there a simple java method to convert that binary representation into its corresponding floating point value?

0
Question by:pdanese
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2

LVL 3

Expert Comment

ID: 8164490
The difference is in Double vs. Float:

0x3f800000 as a double is 5.263544247E-315

0x3f800000 as a float is 1.0

Here's the code:

int binData = 0x3f800000;
float d = Float.intBitsToFloat(binData);
System.out.println(d); // prints 1.0

long binData = 0x3f800000;
double d = Double.longBitsToDouble(binData);
System.out.println(d); // prints 5.263544247E-315

0

LVL 3

Accepted Solution

wide_awake earned 200 total points
ID: 8164525
as for question 2, you could do this:

int binData = Integer.parseInt("111111100000000000000000000000", 2);

Then proceed as above.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.
###### Suggested Courses
Course of the Month15 days, 4 hours left to enroll