Phobox Phobox - 1 year ago 169
Java Question

Convert Hex to Decimal (Lat / Lon)?

I got a latitude and longitude values in hexadecimal:


Converting them to decimal I got...


The problem is, how to get those decimals in the following format using Java?


This is how I actually converting Hex to Decimal:

public static int hex2Decimal(String s) {
long longValue = Long.parseLong(s, 16);
int intValue = (int)longValue;
return intValue;

Answer Source

You need to first parse the hex value to an int, then multiply that by 1e-7 (faster to multiply by 1e-7 that divide by 1e7) divide that by 1e7.
   Thanks to @RolandIllig for pointing out the precision issue.

However, Integer.parseInt() will not allow parsing an 8 digit hex value to a negative integer value, i.e. when the high bit is on.

In Java 8, you'd do this:

public static double hex2Decimal(String s) {
    return Integer.parseUnsignedInt(s, 16) / 1e7;

If your code needs to run on Java versions before 8, do this:

public static double hex2Decimal(String s) {
    return (int)Long.parseLong(s, 16) / 1e7;




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download