简体   繁体   中英

GPS conversion 16digit hexadecimal value to longitude and latitude

From a device I get following type of hexadecimal value 3305511C0424573C (stored in sql database). I need to get the longitude and latitude out of it.

I also get longitude and latitude value in this format (single precision float):

  • longitude = 1083419342
  • latitude = 1112302388

I know the result is:

  • longitude = 4.6146
  • latitude = 51.09688

I tried to code several conversions in C#, but al the outcomes were not correct.

The data from the sql server:

StreamID = 15620, GPS0 = 3305511C0424573C, GPS1 = 0000000000000000, GPS Longitude = 1083419342, GPS Latitude = 1112302388

label = GPS Longitude, Unit = degrees, DataItemType = Single-Precision Float, Precision = 0, BytePosition = 11 label = GPS Latitude, Unit = degrees, DataItemType = Single-Precision Float, Precision = 0, BytePosition = 12

How the data gets in the sql is an unknown for me.

How I use the coordinates:
<script> var XMarker = L.marker([51.09688, 4.6146]).addTo(mymap); </script>
This is what you need to add in your html or webapplication to get extra markers on your map that is created by leafletjs (link: leafletjs.com )

Therefore I need to know the conversion from:

  • 3305511C0424573C -> longitude = 4.6146, latitude = 51.09688
  • 330D262A030C1247 -> longitude = 3.19325013523526, latitude = 51.222186941538

C# answer

public static double GPSCoordinateConversion(string GPSCoordinate)
    {
        double coordinate = 0;
        byte[] _byte = new byte[GPSCoordinate.Length / 2];
        double value1 = 0;
        double value2 = 0;

        for (int i = 0; i < _byte.Length; i++)
        {
            string byteValue = GPSCoordinate.Substring(i * 2, 2);
            _byte[i] = byte.Parse(byteValue, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
        }

        value1 = Convert.ToDouble(_byte[2]) / 100;
        value2 = Convert.ToDouble(_byte[3]) / 10000;

        coordinate = Convert.ToDouble(_byte[0] + ((_byte[1]+value1+value2) / 60));

        return coordinate;
    }

    public static double GetLatitude(string GPSHexCoordinate)
    {
        double latitude = 0;

        latitude = GPSCoordinateConversion(GPSHexCoordinate.Substring(0, 8));

        return latitude;
    }
    public static double GetLongitude(string GPSHexCoordinate)
    {
        double longitude = 0;

        longitude = GPSCoordinateConversion(GPSHexCoordinate.Substring(8, 8));

        return longitude;
    }

Finally I found a way, but I do not know C# so well.

The algorithm:

  • take each number (without the C , which delimit the field)
  • convert it to array of bytes (4 bytes)
  • read such bytes as float (float have standard format).

In C a union between uint32_t and float . In Python struct.unpack('>f', bytearray.fromhex(hex(your_number)[2:]) . C# has BitConverter, and you see an example in bitconverter documentation . I let the other to write a correct C# answer. Note: we still need to test the negative numbers, but they should be ok (few months ago we get a similar question with Australia, so a negative latitude).

The conversion from 3305511C0424573C to longitude = 4.6146 and latitude = 51.09688 is as follows.

  1. Split the 16 character in to two separate strings of 8 characters.

  2. Split the 8 character long string in to a byte array of 4 bytes (2characters long).

  3. The calculation is then as follows: byte[0]+((Byte[1]+(byte[2]/100)+(byte[3]/10000))/60)

Do these steps for longitude and latitude.

    public static double GPSCoordinateConversion(string GPSCoordinate)
    {
        double coordinate = 0;
        byte[] _byte = new byte[GPSCoordinate.Length / 2];
        double value1 = 0;
        double value2 = 0;

        for (int i = 0; i < _byte.Length; i++)
        {
            string byteValue = GPSCoordinate.Substring(i * 2, 2);
            _byte[i] = byte.Parse(byteValue, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
        }

        value1 = Convert.ToDouble(_byte[2]) / 100;
        value2 = Convert.ToDouble(_byte[3]) / 10000;

        coordinate = Convert.ToDouble(_byte[0] + ((_byte[1]+value1+value2) / 60));
        return coordinate;
    }

    public static double GetLatitude(string GPSHexCoordinate)
    {
        double latitude = 0;
        latitude = GPSCoordinateConversion(GPSHexCoordinate.Substring(0, 8));
        return latitude;
    }

    public static double GetLongitude(string GPSHexCoordinate)
    {
        double longitude = 0;
        longitude = GPSCoordinateConversion(GPSHexCoordinate.Substring(8, 8));
        return longitude;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM