Taurus Taurus - 1 year ago 137
Java Question

hex string to decimal conversion

I need to convert the string of hex to decimal in java..

My hex value is "00000156A56BE980".

My required output is 1471654128000

I have done the following in java,

public static void main (String args [])
String hexValue = " 00000156A56BE980 ";
Integer result = Integer.parseInt(hexValue, 16);

but I am getting the following error,

Number format Exception for input string "00000156A56BE980"

I have tried by giving long also the same error coming.. For other hex value it's coming but when we give hex string of larger value it shows the error.

how can we convert to decimal for this number can anyone tell me in solving this issue.

Answer Source

Try it like so

import java.math.*;

class Main {
    public static void main (String args [])
        String hexValue = "00000156A56BE980";
        BigInteger result = new BigInteger(hexValue, 16);

See also this repl.it

The problem is probably because your value does not fit within the value range (-231 to 232-1) of Integer - see the docs

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