longitude reading measured in degrees with a 1x10^-7 degree lsb, signed 2’s complement

前端 未结 3 1304
一向
一向 2021-01-03 05:05

I am receiving data from a gps unit via a udp packet. Lat/Lng values are in hex.

Example Data
13BF71A8 = Latitude (33.1313576)
BA18A506 = Longitude (-1

3条回答
  •  暖寄归人
    2021-01-03 05:40

    Because you are using signed numbers, you need to specify a point at which the hexadecimal code should flip to the bottom. This will be happening at 7FFFFFFF and up. Now update your code to check if the input is greater than this number, and if so, subtract it from the input.

    function convert(h) {
        dec = parseInt(h, 16);
        return (dec < parseInt('7FFFFFFF', 16)) ?
            dec * 0.0000001 :
            0 - ((parseInt('FFFFFFFF', 16) - dec) * 0.0000001);
    }
    

    The only reason your example worked is because the output was expected to be positive.


    As AlexWien mentioned in the comments: Since parsing 7FFFFFFF and FFFFFFFF are giving the same integers every time, you could store them as constants. Their values are 2147483647 and 4294967295 respectively.

提交回复
热议问题