Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 563

# hex / binary to floating point representation

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
pdanese
• 2
1 Solution

Commented:
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

Commented:
as for question 2, you could do this:

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

Then proceed as above.
0

## Featured Post

• 2
Tackle projects and never again get stuck behind a technical roadblock.