C#如何保存big big int?

时间:2018-02-27 21:37:47

标签: c# int biginteger

我需要从字符串转换此值:4248035245857302861304475122262852382232831183377907185400973044372526725256648804647567360并将其保存在int中。长,Int64数据类型不起作用。我不能使用BigInteger。双人不能分。一些建议?

    string valorLido;            
            while ((valorLido = Console.ReadLine()) != null)
            {
                int leapYear = 0;
                int huluculuFestival = 0;
                int bulukuluFestival = 0;

                long ano = long.Parse(valorLido); 

                if ((ano % 4 == 0) && (ano % 100 != 0 || ano % 400 == 0))
                    leapYear = 1;
                if (ano % 15 == 0)
                    huluculuFestival = 1;
                if (leapYear == 1 && ano % 55 == 0)
                    bulukuluFestival = 1;

                if(leapYear == 1)
                    Console.WriteLine("This is leap year.");
                if(huluculuFestival == 1)
                    Console.WriteLine("This is huluculu festival year.");
                if(bulukuluFestival == 1)
                    Console.WriteLine("This is bulukulu festival year.");
                if((leapYear != 1) && (huluculuFestival != 1) && (bulukuluFestival != 1)) 
                {
                    Console.WriteLine("This is an ordinary year.");
                }
            }

输入: 4248035245857302861304475122262852382232831183377907185400973044372526725256648804647567360

输出:

This is leap year. This is huluculu festival year. This is bulukulu festival year.

1 个答案:

答案 0 :(得分:6)

如果您需要以可重用的方式表示大整数数据,例如要将它保存在数据库中,您可以将其保存为字符串形式,也可以将其序列化为字节数组。

string data = "927349273497234...";
BigInteger big = BigInteger.Parse(data);
byte[] serialized = big.ToByteArray();

现在您可以使用此字节数组将其保存到数据库或发送它。

稍后,您可以使用其接收字节数组的构造函数重新创建BigInteger对象:

byte[] data = ...
BigInteger later = new BigInteger(data);