简体   繁体   中英

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

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 withLong.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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM