How to convert hex to longi and lati? - gps

Recently I got a GPS to receive data as hexadecimal values and I want to get the data from hex to dec directly.
For example:
Lati: DB0A51FC = 22.3368658
Long: FCBD003B = 114.1758175
Alti: 6A3DFF9E = 16.844
The above conversion may not be exact as I run 2 different programs to get the data in the same place, which should be only slightly different. However, I want to get the GPS data in degree from hex directly for further usage.
I would like to know how the conversion works as the hex numbers give -ve values for direct conversion to decimal.
Thanks for all the help!

Related

Increase hexadecimal places

I'm trying to increase the number of places a hexadecimal is using.
I've managed to do this when I'm using String, by using String.format("%04X", bytes).
But my source is of ByteArray and I would like to avoid partially converting it to String, change the number of places and then convert it back into bytes.
So the idea is in my source I have something like this 0D which I want to be 000D in my final representation. But I want to achieve this before fully converting my source to hex representation.
Is there a more direct way to achieve this?

Kotlin: Convert Hex String to signed integer via signed 2's complement?

Long story short, I am trying to convert strings of hex values to signed 2's complement integers. I was able to do this in a single line of code in Swift, but for some reason I can't find anything analogous in Kotlin. String.ToInt or String.ToUInt just give the straight base 16 to base 10 conversion. That works for some positive values, but not for any negative numbers.
How do I know I want the signed 2's complement? I've used this online converter and according to its output, what I want is the decimal from signed 2's complement, not the straight base 16 to base 10 conversion that's easy to do by hand.
So, "FFD6" should go to -42 (correct, confirmed in Swift and C#), and "002A" should convert to 42.
I would appreciate any help or even any leads on where to look. Because yes I've searched, I've googled the problem a bunch and, no I haven't found a good answer.
I actually tried writing my own code to do the signed 2's complement but so far it's not giving me the right answers and I'm pretty at a loss. I'd really hope for a built in command that does it instead; I feel like if other languages have that capability Kotlin should too.
For 2's complement, you need to know how big the type is.
Your examples of "FFD6" and "002A" both have 4 hex digits (i.e. 2 bytes).  That's the same size as a Kotlin Short.  So a simple solution in this case is to parse the hex to an Int and then convert that to a Short.  (You can't convert it directly to a Short, as that would give an out-of-range error for the negative numbers.)
"FFD6".toInt(16).toShort() // gives -42
"002A".toInt(16).toShort() // gives 42
(You can then convert back to an Int if needed.)
You could similarly handle 8-digit (4-byte) values as Ints, and 2-digit (1-byte) values as Bytes.
For other sizes, you'd need to do some bit operations.  Based on this answer for Java, if you have e.g. a 3-digit hex number, you can do:
("FD6".toInt(16) xor 0x800) - 0x800 // gives -42
(Here 0x800 is the three-digit number with the top bit (i.e. sign bit) set.  You'd use 0x80000 for a five-digit number, and so on.  Also, for 9–16 digits, you'd need to start with a Long instead of an Int.  And if you need >16 digits, it won't fit into a Long either, so you'd need an arbitrary-precision library that handled hex…)

Formatting Short Text as Numbers

I've got a column called Amount, with a lot of numbers looking like this:
67000.00000000000000000000
Some of the columns have 2 numbers after the decimal that need to be retained.
Which should amount to $67,000.00
But my problem is, when I format it into currency or numbers, I get MUCH larger numbers than i would like, looking like this:
6.700.000.000.000.000.000.000.000,00
How can I get it into the right format?
Edit: For this scenario, the user was using ACC2013 and the Field Type was Short Text. The method of conversion that succeeded was : CCur(Val(FieldNameHere))
CCur(YourFieldName)
This will convert it to a currency format.
CLng(YourFieldName)
This will convert it to a long integer format. (It will cut off the decimals)
If you're looking for a reference, Microsoft has a few examples and goes into brief detail about some of these conversion functions.
CCur(Replace("67000.00000000000000000000", ".", Format(0, ".")))
You have to replace point symbol to actual decimal separator before conversion. Because you can't know actual seprator, choosen in regional settings, you have to find it out - and such Format() operation does dirty work.

How to get float value as it is from the text box in objective c

Can any one please help me how to get float value as it is from text box
for Ex: I have entered 40.7
rateField=[[rateField text] floatValue];
I am getting rateField value as 40.7000008 but I want 40.7 only.
please help me.
thanks in advance
Thanks Every body,
I tried all the possibilities but I am not able to get what I want. I am not looking to print the value to convert into string.I want to use that value for computation. If i use Number Formatter again when i am converting from number to float it is giving same problem.So i want float value only but it should be whatever i have given in the text box it should not be padded with any values.This is my requirement.Please help me.
thanks&regards Balu
Thanks Every body,
I tried all the possibilities but I am not able to get what I want. I am not looking to print the value to convert into string.I want to use that value for computation. If i use Number Formatter again when i am converting from number to float it is giving same problem.So i want float value only but it should be whatever i have given in the text box it should not be padded with any values.This is my requirement.Please help me.
thanks&regards
Balu
This is ok. There is not guaranteed that you will get 40.7 if you will use even double.
If you want to output 40.7 you can use %.1f or NSNumberFormatter
Try using a double instead. Usually solves that issue. Has to do with the storage precision.
double dbl = [rateField.text doubleValue];
When using floating point numbers, these things can happen because of the way the numbers are stored in binary format in the computers memory.
It's similar to the way 1/3 = 0.33333333333333... in decimal numbers.
The best way to deal with this is to use number formatters in the textbox that displays the value.
You are already resolved float value.
Floating point numbers have limited precision. Although it depends on
the system, float relative error due to rounding will be around 1.1e-8
Non elementary arithmetic operations may give larger errors, and, of
course, error progragation must be considered when several operations
are compounded.
Additionally, rational numbers that are exactly representable as
floating point numbers in base 10, like 0.1 or 0.7, do not have an
exact representation as floating point numbers in base 2, which is
used internally, no matter the size of the mantissa. Hence, they
cannot be converted into their internal binary counterparts without a
small loss of precision. This can lead to confusing results: for
example, floor((0.1+0.7)*10) will usually return 7 instead of the
expected 8, since the internal representation will be something like
7.9999999999999991118....
So if you're using those numbers for output, you should use some rounding mechanism, even for double values.

Using Google Weather API with Lat and Lon - how to format?

I want to use the Google Weather API - by passing lat and long values. I am storing these values, however it seems Google needs these values in different format.
i.e. For the town of McTavish I have values of 45.5 and -73.583
This works here: http://api.wunderground.com/auto/wui/geo/WXCurrentObXML/index.xml?query=45.5,-73.583
But when I use these data for Google API, it does not work: See: www.google.com/ig/api?weather=,,,45.5,-73.583
Any help appreciated. I would prefer to use the Google Data.
UPDATED ANSWER:
I have just noticed some OTHER irregularities with Google's Weather API. In ANY case, you need to have 8 numerical digits, in addition to the negative sign, if it applies. See the following code block (Java-based) for proper formatting. (Not the perfect algorithm, but just a quick example so that you can see the results)
lat = lat.replace(".", "");
while(lat.length() < 9)
lat = lat.concat("0");
if(lat.contains("-"))
lat = lat.substring(0, 9);
else
lat = lat.substring(0, 8);
ORIGINAL RESPONSE:
Paul, the trick about Google's Weather API is that you don't use the coordinates as received by traditional latitude/longitude. Instead, you parse out the decimal points. Additionally, a "fun quirk" of Google's Weather API seems to be a requirement that the data come in as a 7- to 8-digit string. So, for instance, 45.5 should really be 45.50000, and -73.583 should really be -73.58300. This length of 7-8 digits does NOT seem to include the negative sign (-) in front of any negative coordinates.
So, your 45.5(0000) becomes 4550000, and your -73.583(00) becomes -7358300. So the final URL would be:
http://www.google.com/ig/api?weather=,,,4550000,-7358300
Note that again, 7-8 digits means 4550000 or 45500000 would be acceptable, as would -7358300 or -73583000.
I only found out about the 7-8 digit length when I saw your question--I tried entering the data into my weather parsing program, and found that 455,-73583 does not yield proper data.
Note that this is by my unofficial experimentation, and not by official documentation, so there may be other quirks to be discovered.
It's much simpler - latitude & longitude should be multiplied by one million
In my opinion longitude an latitude must be coded in _e6 format (six last digits must be the decimal part of the lon/lat string passed to the API). Then you must adjust first decimals to a length of six, you add 0s if you have fewer than six decimals, and you clip it to 6, if you have more. For the int part if you have just one character you must add a zero first, for two or three digits, you don't need to do anything.
Examples:
1.1234 must be coded as: -01123400
112.2345 must be coded as: 112234500
34.123456 must be coded as
Here you have a explanation with examples and a the php source code (the blog is written in Spanish).
As Miguel said it is the last 6 digits that should be the decimal places. So the correct code is as simple as....
string.Format("http://www.google.com/ig/api?weather=,,,{0:0},{1:0}",
(latitude * 1000000),
(longitude * 1000000));