Converting a GPS Coordinate to a decimal with C#

1k views Asked by At

I need to parse a byte array to find latitude and longitude. I have four bytes {0x91,0x2a,0xb4,0x41} that I use to calculate the latitude. The response should be 22.52078.

How can I find the result ? I use C# to do the parse.

1

There are 1 answers

0
Lucas Trzesniewski On BEST ANSWER

Looks like a float (4 bytes)...

var data = new byte[] { 0x91, 0x2a, 0xb4, 0x41 };
var coord = BitConverter.ToSingle(data, 0);

Console.WriteLine(coord); // Output is 22.52078

Demo.

Don't confuse float/double/decimal:

Compared to floating-point types, the decimal type has more precision and a smaller range, which makes it appropriate for financial and monetary calculations.

double (and float to a lesser extent) are a better fit for physical measures.