C# BigInteger ToByteArray

Description

BigInteger ToByteArray Converts a BigInteger value to a byte array.

Syntax

BigInteger.ToByteArray has the following syntax.


public byte[] ToByteArray()

Returns

BigInteger.ToByteArray method returns

Example

The following example illustrates how some BigInteger values are represented in byte arrays.


/*from ww w  .j a v a  2s .  c o  m*/
using System;
using System.Numerics;

public class Example
{
   static byte[] bytes;

   public static void Main()
   {
      BigInteger[] numbers = { Int64.MinValue, Int64.MaxValue, 
                               BigInteger.Parse("90123123981293054321") };
      foreach (BigInteger number in numbers)
      {
         bytes = number.ToByteArray();
         Console.Write("{0} ({1}) -> ", number, number.ToString(GetSpecifier()));
         Console.Write("{0} bytes: ", bytes.Length);
         foreach (byte byteValue in bytes)
            Console.Write("{0:X2} ", byteValue);

         Console.WriteLine();
      }   
   }

   private static string GetSpecifier()
   {
      return "X" + (bytes.Length * 2).ToString();
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Numerics »




BigInteger
Complex