Search code examples
javaieee-754

How to convert hex value to single precision floating point number and also in double precision floating point number according to the IEEE-754 format


Can someone please tell me how i can convert a Hex string to its corresponding single precision or double precision floating point number according to the IEEE-754 format in java?

For example : "4682E989" and "A4703D4A0B0B7441"

BR suppi


Solution

  • 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 longfrom 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.