Can someone please tell me how i can convert a Hex string to its correspond开发者_JAVA技巧ing single precision or double precision floating point number according to the IEEE-754 format in java?
For example : "4682E989" and "A4703D4A0B0B7441"
BR suppi
See Double.longbitsToDouble
.
Returns the double value corresponding to a given bit representation. The argument is considered to be a representation of a floating-point value according to the IEEE 754 floating-point "double format" bit layout.
You can get a long
from the hex representation with Long.parseLong
new BigInteger(s, 16).longValue()
(as Peter Lawrey points out). Long.parseLong
won't do because it fails on numbers larger than 2^63-1.
Here is the example with correct answer .
Hex value = 440B1831
Expected Binary = 1000100000010110001100000110001
Expected float= 556.378
Long decimal =new BigInteger("440B1831", 16).longValue();
String binary1 = java.lang.Long.toBinaryString(decimal);
System.out.println(binary1);
int intBits = Integer.parseInt(binary1, 2);
float myFloat = Float.intBitsToFloat(intBits);
System.out.println( myFloat);
精彩评论