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

谁都会走 提交于 2019-11-30 15:41:27

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.

Lat and Long use the same algorithm for conversion. Your latitude accidentaly wokred because it is positive (33.13)

The test Longitude is negative, which makes the error in the conversion algorithm visible, as usually with negative numbers.

Deepak Negi

use this for 2 compliment

private static Decimal ParseHexStringToDouble(string hexNumber) {
    long result = 0;
    result = int.Parse(hexNumber, System.Globalization.NumberStyles.HexNumber);
    return result;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!