How to initialize BigInteger from an array of unsigned 32-bit integers in C#

1 Answer

0 votes
using System;
using System.Numerics;
 
public class Program
{
    public static void Main()
    {
        uint[] arr = { 0, 16384, 65536, UInt32.MaxValue };
		
		foreach (uint val in arr) {
   			BigInteger bi1 = new BigInteger(val);
   			BigInteger bi2 = val;
   			if (bi1.Equals(bi2)) {
      			Console.WriteLine("Both methods create a BigInteger and the value is {0:N0}", bi1);
			}
   			else {
      			Console.WriteLine("{0:N0} != {1:N0}", bi1, bi2);
			}
		}
    }
}

 
/*
run:
 
Both methods create a BigInteger and the value is 0
Both methods create a BigInteger and the value is 16,384
Both methods create a BigInteger and the value is 65,536
Both methods create a BigInteger and the value is 4,294,967,295
 
*/


 



answered Jun 10, 2024 by avibootz
...