原始类型(例如Int32、Int64)具有有限的长度,对于这么大的数字是不够的。例如:
数据类型 最大正值
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
您的号码 305,802,052,421,002,911,840,647,389,720,929,531,201
在这种情况下,您需要 128 位来表示该数字。在 .NET Framework 4.0 中,有一种用于任意大小整数的新数据类型 System.Numerics.BigInteger。您无需指定任何大小,因为它将由数字本身推断(这意味着您甚至可能在执行时得到OutOfMemoryException,例如,两个非常大的乘积数字)。
回到你的问题,首先解析你的十六进制数:
string bigNumberAsText = "e60f553e42aa44aebf1d6723b0be7541";
BigInteger bigNumber = BigInteger.Parse(bigNumberAsText,
NumberStyles.AllowHexSpecifier);
然后简单地打印到控制台:
Console.WriteLine(bigNumber.ToString());
您可能有兴趣计算需要多少位来表示任意数字,请使用此函数(如果我记得原始实现来自 C Numerical Recipes):
public static uint GetNeededBitsToRepresentInteger(BigInteger value)
{
uint neededBits = 0;
while (value != 0)
{
value >>= 1;
++neededBits;
}
return neededBits;
}
然后计算一个写成字符串的数字所需的大小:
public static uint GetNeededBitsToRepresentInteger(string value,
NumberStyles numberStyle = NumberStyles.None)
{
return GetNeededBitsToRepresentInteger(
BigInteger.Parse(value, numberStyle));
}