开发者

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

开发者 https://www.devze.com 2023-03-04 03:55 出处:网络
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?

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


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号