Search code examples
c#sqlsql-servergps

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;
    }

Solution

  • 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;
        }