Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert a large string to integer in java?

Given the following string:

3132333435363738396162636465666768696a6b6c6d6e6f70

I converted the string to hex and now i want to file write it as hex not a string. I tried converting it to int but Integer.parseInt converts up to 4 only and if go beyond that it would give error already.

like image 522
user2090151 Avatar asked Feb 20 '13 07:02

user2090151


People also ask

How do I convert a String to a large number in java?

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... } }

How do I convert a String to a large number?

You convert a string to a number by calling the Parse or TryParse method found on numeric types ( int , long , double , and so on), or by using methods in the System. Convert class.

Can you turn a String into an int java?

We can convert String to an int in java using Integer. parseInt() method. To convert String into Integer, we can use Integer. valueOf() method which returns instance of Integer class.

What is the syntax for converting String to Integer?

The method generally used to convert String to Integer in Java is parseInt() of String class.


1 Answers

Have you tried the BigInteger constructor taking a string and a radix?

BigInteger value = new BigInteger(hex, 16);

Sample code:

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...
    }
}

Output:

308808885829455478403317837970537433512288994552567292653424
like image 126
Jon Skeet Avatar answered Oct 02 '22 14:10

Jon Skeet