We help IT Professionals succeed at work.

# Hex to Decimal Conversion Problem

on
When I use the code below to convert hex to decimal.
int a = 0;
str = "00005AF3107A3FDF"
a = Integer.parseInt(str, 16);

The result should be 999999999999.67 but i got error message "java.lang.Integer.ParseInt". It looks like the Integer not able to handle this large number.

How can I resolve this issue?

Thanks,

Gary
Comment
Watch Question

## View Solution Only

Commented:
Can you use a long variable.

Commented:
999999999999.67? Doesn't look like an integer to me. If it's a double then I'm afraid Java doesn't have any methods to convert hex directly to double. I'd convert it into a byte array then use a ByteBuffer to read a double.

Commented:
No, wait, it doesn't look like a double either. Parsed as Long, however, it gives 99999999999967.

``````String s = "00005AF3107A3FDF";
System.out.println(Long.parseLong(s, 16));
``````
Awarded 2011
Awarded 2011
Commented:
You can use methods form this link:

and it converts to long and back to hex (see output)

``````public class HexLong {
public static void main(String[] args) {

String str = "00005AF3107A3FDF";
long a = Long.parseLong(str, 16);
System.out.println("long: " + a);

long b =  99999999999967L;

System.out.println(fromLong(b));

}

private final static char[] HEX = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};

public static long toLong(String hexadecimal) throws NumberFormatException{
char[] chars;
char c;
long value;
int i;
byte b;

throw new IllegalArgumentException();

value = 0;
b = 0;
for (i = 0; i < 16; i++) {
c = chars[i];
if (c >= '0' && c <= '9') {
value = ((value << 4) | (0xff & (c - '0')));
} else if (c >= 'A' && c <= 'F') {
value = ((value << 4) | (0xff & (c - 'A' + 10)));
} else {
throw new NumberFormatException("Invalid hex character: " + c);
}
}

return value;
}

public static String fromLong(long value) {
char[] hexs;
int i;
int c;

hexs = new char[16];
for (i = 0; i < 16; i++) {
c = (int)(value & 0xf);
hexs[16-i-1] = HEX[c];
value = value >> 4;
}
return new String(hexs);
}

}
``````

output:

``````long: 99999999999967
00005AF3107A3FDF
``````
Awarded 2011
Awarded 2011

Commented:
You can also use BigInteger class:

``````import java.math.BigInteger;

public class Convert {

public static void main(String ] args) {

String str = "00005AF3107A3FDF";
BigInteger bi = new BigInteger(str,16);

System.out.println("bi: " + bi);

System.out.println("bi: " + bi.doubleValue());

}
}
``````

``````bi: 99999999999967
bi: 9.9999999999967E13
``````
Awarded 2011
Awarded 2011

Commented:
And of couse can be converted back to hex
(and small corretion; this one was exactly pasted as compiled and tested)

``````import java.math.BigInteger;

public class Convert {

public static void main(String[ ] args) {

String str = "00005AF3107A3FDF";
BigInteger bi = new BigInteger(str,16);

System.out.println("bi: " + bi);

System.out.println("bi: " + bi.doubleValue());

System.out.println(" convert back to hex: " + bi.toString(16));

}

}
``````

output:
``````bi: 99999999999967
bi: 9.9999999999967E13
convert back to hex: 5af3107a3fdf
``````

Commented:
Thanks for you quick respond and perfert solution.