Java, convert floating point hexadecimal value

Asked

Viewed 342 times

5

I am developing an application that receives data from a GPS board, the data is received by bluetooth in hexadecimal, the latitude and longitude values are floating points of 64 bits, which follows the standard IEEE 754.

I can already capture the hexadecimal values I need for a String variable. For example:

b3baf6e3530b38c0 (latitude)

ff198e4d752f4ac0 (longitude)

But I’m not able to transform this string with the hexadecimal value in the latitude and longitude values, for example: -24.123456789 and -52.123456789.

Does anyone know any way to interpret the hexadecimal value of a floating point, for a double variable?

  • Just to confirm, for the two values of your example the desired doubles would be respectively -1.677999117094125E-59 and -1.7525333784680737E304?

  • 1

    @Anthonyaccioly I don’t know where Romão got these hexadecimal numbers from, but the two decimal numbers you suggested must be wrong, judging by their exponents.

  • Yeah, I even checked against a calculator. Maybe the bits are inverted somehow? :)

  • @Anthonyaccioly Yes, I’m also trying to do some mathematical tests to see if this can somehow produce a feasible number. The fact that they both end up with c0 makes me suspect about some reordering.

  • @Anthonyaccioly The IEEE 754 standard defines the returned hexa, as I saw in: https://pt.wikipedia.org/wiki/IEEE_754, hexa is composed of: Signal, exponent and mantissa.

  • 1

    So I apologize, but I found the answer to why I can’t convert. I’m getting the mirrored values, so if I reverse the hexadecimal sequence and try to convert normally it returns the correct values. b3baf6e3530b38c0 is actually c0380b53e3f6bab3

Show 1 more comment

1 answer

3

The answer is that the values received from the board that transmits the GPS are coming in a mirror shape, so it was not getting the real values. then the value b3baf6e3530b38c0 is actually c0380b53e3f6bab3 To perform the conversion, I used the following code:

public double hexToDouble(String hexvalue){
    return Double.longBitsToDouble(new BigInteger(hexvalue,16).longValue());
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.