Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the most effective way to create BigInteger instance from int value?

I have a method (in 3rd-party library) with BigInteger parameter:

public void setValue (BigInteger value) { ... }

I don't need 'all its power', I only need to work with integers. So, how can I pass integers to this method? My solution is to get string value from int value and then create BigInteger from string:

int i = 123;
setValue (new BigInteger ("" + i));

Are there any other (recommended) ways to do that?

like image 720
Roman Avatar asked Apr 15 '10 14:04

Roman


People also ask

How do you create a BigInteger?

Use different radix to create BigInteger from String BigInteger(String val, int radix) translates the String representation of a BigInteger in the specified radix into a BigInteger. You can set the radix for the string value passed in with new BigInteger(String val, int radix) .

What are the ways can we adopt to initialize a BigInteger number?

Initializing a BigInteger from a String Now that we are able to represent numerical numbers using Strings, we have raised the maximum number we can initialize a big integer to a number with 2147483647 digits. This is because the maximum length of a String is Integer. MAX_VALUE.

Which of the following is a correct way of initializing a BigInteger variable?

The simpler "proper" way would be String.


4 Answers

BigInteger.valueOf(i);
like image 76
Michael Borgwardt Avatar answered Oct 24 '22 18:10

Michael Borgwardt


Use the static method BigInteger.valueOf(long number). int values will be promoted to long automatically.

like image 23
Bozhidar Batsov Avatar answered Oct 24 '22 19:10

Bozhidar Batsov


You can use this static method: BigInteger.valueOf(long val)

like image 38
Petar Minchev Avatar answered Oct 24 '22 20:10

Petar Minchev


setValue(BigInteger.valueOf(123L));
like image 28
Paul Croarkin Avatar answered Oct 24 '22 19:10

Paul Croarkin