经度读数以 1x10^-7 度 lsb 为单位测量,带符号 2 的补码

我正在通过 udp 数据包从 gps 单元接收数据.Lat/Lng 值是十六进制的.

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

示例数据
13BF71A8 = 纬度 (33.1313576)
BA18A506 = 经度 (-117.2790010)

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

文档解释说,经度/纬度读数以度为单位测量,1x10^-7 度 lsb,带符号 2 的补码.

The documentation explains that longitude/latitude readings are measured in degrees with a 1x10^-7 degree lsb, signed 2’s complement.

对于纬度,我可以使用以下公式进行转换:
13BF71A8 = 331313576 * 0.0000001 = 33.1313576

For the Latitude I can convert using this formula:
13BF71A8 = 331313576 * 0.0000001 = 33.1313576

此代码适用于 Lat 但不适用于 Lng:

This code works for Lat but not for Lng:

function convertLat(h){
var latdec = parseInt(h,16);
    var lat = latdec * 0.0000001;
    return lat;
}

console.log("LAT: " + convertLat("13BF71A8"));

我在转换经度值时遇到问题.有谁知道如何转换经度?

I am having trouble converting the Longitude value. Does anyone know how to convert the Longitude?

推荐答案

因为你使用的是有符号数,所以你需要指定一个十六进制代码应该翻转到底部的点.这将发生在 7FFFFFFF 及以上.现在更新您的代码以检查输入是否大于此数字,如果是,则从输入中减去它.

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.

正如评论中提到的 AlexWien:由于每次解析 7FFFFFFFFFFFFFFF 都会给出相同的整数,因此您可以将它们存储为常量.它们的值分别是 21474836474294967295.

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.

相关文章