Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a string into BigInteger

I have the following code that creates a very big number (BigInteger) which is converted then into a string.

// It's a console application.
BigInteger bi = 2;
for (int i = 0; i < 1234; i++)
{
   bi *= 2;
}
string myBigIntegerNumber = bi.ToString();
Console.WriteLine(myBigIntegerNumber);

I know that for converting to int we can use Convert.ToInt32 and converting to long we use Convert.ToInt64, but what's about converting to BigInteger?

How can I convert a string (that represents a very very long number) to BigInteger?

like image 309
Ionică Bizău Avatar asked Jan 06 '13 15:01

Ionică Bizău


People also ask

How to convert string to BigInteger in Java?

Use BigInteger.Parse () method. Converts the string representation of a number in a specified style to its BigInteger equivalent. Also you can check BigInteger.TryParse () method with your conversation is successful or not.

What is a BigInteger in C++?

The BigInteger is commonly used for working with very large numerical values, which are usually the result of arbitrary arithmetic calculations. 2. Converting Decimal (Base 10) Integer Strings To convert a decimal String to BigInteger, we'll use the BigInteger (String value) constructor:

How do you convert a decimal string to a integer string?

Converting Decimal (Base 10) Integer Strings To convert a decimal String to BigInteger, we'll use the BigInteger (String value) constructor: String inputString = "878" ; BigInteger result = new BigInteger (inputString); assertEquals ( "878", result.toString ());

How to check if a BigInteger conversion is successful?

Also you can check BigInteger.TryParse () method with your conversation is successful or not. Tries to convert the string representation of a number to its BigInteger equivalent, and returns a value that indicates whether the conversion succeeded.


1 Answers

Use BigInteger.Parse() method.

Converts the string representation of a number in a specified style to its BigInteger equivalent.

BigInteger bi = 2;
for(int i = 0; i < 1234; i++)
{
    bi *= 2;
}

var myBigIntegerNumber = bi.ToString();
Console.WriteLine(BigInteger.Parse(myBigIntegerNumber));

Also you can check BigInteger.TryParse() method with your conversation is successful or not.

Tries to convert the string representation of a number to its BigInteger equivalent, and returns a value that indicates whether the conversion succeeded.

like image 107
Soner Gönül Avatar answered Nov 09 '22 01:11

Soner Gönül