Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arbitrarily large integers in C#

How can I implement this python code in c#?

Python code:

print(str(int(str("e60f553e42aa44aebf1d6723b0be7541"), 16)))

Result:

305802052421002911840647389720929531201

But in c# I have problems with big digits.

Can you help me?

I've got different results in python and c#. Where can be mistake?

like image 235
pic0 Avatar asked Apr 24 '12 07:04

pic0


People also ask

What is arbitrarily large number?

In mathematics, the phrases arbitrarily large, arbitrarily small and arbitrarily long are used in statements to make clear of the fact that an object is large, small and long with little limitation or restraint, respectively.

What are large integers?

A large integer is a binary integer with a precision of 31 bits. The range of large integers is -2147483648 to +2147483647.

How do you handle large integers in C++?

In C++, we can use large numbers by using the boost library. This C++ boost library is widely used library. This is used for different sections. It has large domain of applications.

What is big integers in C++?

In C/C++ the number of digits a long long int can have is a maximum of 20. And the question is to store the 22 digit number which is not easy to store in any kind of primitive type.


2 Answers

Primitive types (such as Int32, Int64) have a finite length that it's not enough for such big number. For example:

Data type                                     Maximum positive value
Int32                                                  2,147,483,647
UInt32                                                 4,294,967,295
Int64                                      9,223,372,036,854,775,808
UInt64                                    18,446,744,073,709,551,615
Your number      305,802,052,421,002,911,840,647,389,720,929,531,201

In this case to represent that number you would need 128 bits. With .NET Framework 4.0 there is a new data type for arbitrarily sized integer numbers System.Numerics.BigInteger. You do not need to specify any size because it'll be inferred by the number itself (it means that you may even get an OutOfMemoryException when you perform, for example, a multiplication of two very big numbers).

To come back to your question, first parse your hexadecimal number:

string bigNumberAsText = "e60f553e42aa44aebf1d6723b0be7541";
BigInteger bigNumber = BigInteger.Parse(bigNumberAsText,
    NumberStyles.AllowHexSpecifier);

Then simply print it to console:

Console.WriteLine(bigNumber.ToString());

You may be interested to calculate how many bits you need to represent an arbitrary number, use this function (if I remember well original implementation comes from C Numerical Recipes):

public static uint GetNeededBitsToRepresentInteger(BigInteger value)
{
   uint neededBits = 0;
   while (value != 0)
   {
      value >>= 1;
      ++neededBits;
   }

   return neededBits;
}

Then to calculate the required size of a number wrote as string:

public static uint GetNeededBitsToRepresentInteger(string value,
   NumberStyles numberStyle = NumberStyles.None)
{
   return GetNeededBitsToRepresentInteger(
      BigInteger.Parse(value, numberStyle));
}
like image 58
Adriano Repetti Avatar answered Sep 28 '22 04:09

Adriano Repetti


If you just want to be able to use larger numbers there is BigInteger which has a lot of digits.

like image 42
annonymously Avatar answered Sep 28 '22 06:09

annonymously