Arbitrarily large integers in C#

你说的曾经没有我的故事 提交于 2019-11-27 06:51:54

问题


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?


回答1:


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));
}



回答2:


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




回答3:


To find the number of bits you need to store a BigInteger N, you can use:

BigInteger N = ...;
int nBits = Mathf.CeilToInt((float)BigInteger.Log(N, 2.0));


来源:https://stackoverflow.com/questions/10293603/arbitrarily-large-integers-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!