Converting from Integer, to BigInteger

JavaBiginteger

Java Problem Overview


I was wondering if there was any way to convert a variable of type Integer, to BigInteger. I tried typecasting the Integer variable, but i get an error that says inconvertible type.

Java Solutions


Solution 1 - Java

The method you want is BigInteger#valueOf(long val).

E.g.,

BigInteger bi = BigInteger.valueOf(myInteger.intValue());

Making a String first is unnecessary and undesired.

Solution 2 - Java

converting Integer to BigInteger

		BigInteger big_integer = BigInteger.valueOf(1234);

converting BigInteger back to Integer

int integer_value = big_integer.intValue();

converting BigInteger back to long

long long_value = big_integer.longValue();

converting string to BigInteger

		BigInteger bigInteger = new BigInteger("1234");

converting BigInteger back to string

		String string_expression = bigInteger.toString();

Solution 3 - Java

You can do in this way:

    Integer i = 1;
    new BigInteger("" + i);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSteffan HarrisView Question on Stackoverflow
Solution 1 - JavajbindelView Answer on Stackoverflow
Solution 2 - JavaFabala DibbaseyView Answer on Stackoverflow
Solution 3 - JavaGiorgios KaragounisView Answer on Stackoverflow