How to convert a large string to an integer in java?

Given the following line:

3132333435363738396162636465666768696a6b6c6d6e6f70

I converted the string to hex, and now I want the file to write it as a hex string. I tried converting it to int, but it Integer.parseIntonly converts to 4, and if it goes beyond that, it will already give an error.

+5
source share
3 answers

Have you tried the constructor BigIntegerwith string and radius ?

BigInteger value = new BigInteger(hex, 16);

Code example:

import java.math.BigInteger;

public class Test {

    public static void main(String[] args) {
        String hex = "3132333435363738396162636465666768696a6b6c6d6e6f70";
        BigInteger number = new BigInteger(hex , 16);
        System.out.println(number); // As decimal...
    }
}

Conclusion:

308808885829455478403317837970537433512288994552567292653424
+8
source

Use BigInteger , one of the constructors takes a radius.

BigInteger value = new BigInteger(hexString, 16);
+1
source

Try:

new BigInteger("3132333435363738396162636465666768696a6b6c6d6e6f70", 16)
+1
source

All Articles