Export (0) Print
Expand All

BigInteger Constructor (Byte[])

Initializes a new instance of the BigInteger structure using the values in a byte array.

This API is not CLS-compliant. 

Namespace:  System.Numerics
Assembly:  System.Numerics (in System.Numerics.dll)

[CLSCompliantAttribute(false)]
public BigInteger(
	byte[] value
)

Parameters

value
Type: System.Byte[]

An array of byte values in little-endian order.

ExceptionCondition
ArgumentNullException

value is null.

The individual bytes in the value array should be in little-endian order, from lowest-order byte to highest-order byte. For example, the numeric value 1,000,000,000,000 is represented as shown in the following table:

Hexadecimal string

E8D4A51000

Byte array (lowest index first)

00 10 A5 D4 E8 00

Most methods that convert numeric values to byte arrays, such as BigInteger.ToByteArray and BitConverter.GetBytes, return byte arrays in little-endian order.

The constructor expects positive values in the byte array to use sign-and-magnitude representation, and negative values to use two's complement representation. In other words, if the highest-order bit of the highest-order byte in value is set, the resulting BigInteger value is negative. Depending on the source of the byte array, this may cause a positive value to be misinterpreted as a negative value. Byte arrays are typically generated in the following ways:

  • By calling the BigInteger.ToByteArray method. Because this method returns a byte array with the highest-order bit of the highest-order byte in the array set to zero for positive values, there is no chance of misinterpreting a positive value as negative. Unmodified byte arrays created by the ToByteArray method always successfully round-trip when they are passed to the BigInteger(Byte[]) constructor.

  • By calling the BitConverter.GetBytes method and passing it a signed integer as a parameter. Because signed integers handle both sign-and-magnitude representation and two's complement representation, there is no chance of misinterpreting a positive value as negative.

  • By calling the BitConverter.GetBytes method and passing it an unsigned integer as a parameter. Because unsigned integers are represented by their magnitude only, positive values can be misinterpreted as negative values. To prevent this misinterpretation, you can add a zero-byte value to the end of the array. The example in the next section provides an illustration.

  • By creating a byte array either dynamically or statically without necessarily calling any of the previous methods, or by modifying an existing byte array. To prevent positive values from being misinterpreted as negative values, you can add a zero-byte value to the end of the array.

If value is an empty Byte array, the new BigInteger object is initialized to a value of BigInteger.Zero. If value is null, the constructor throws an ArgumentNullException.

The following example instantiates a BigInteger object from a 5-element byte array whose value is {5, 4, 3, 2, 1}. It then displays the BigInteger value, represented as both decimal and hexadecimal numbers, to the console. A comparison of the input array with the text output makes it clear why this overload of the BigInteger class constructor creates a BigInteger object whose value is 4328719365 (or 0x102030405). The first element of the byte array, whose value is 5, defines the value of the lowest-order byte of the BigInteger object, which is 0x05. The second element of the byte array, whose value is 4, defines the value of the second byte of the BigInteger object, which is 0x04, and so on.

byte[] bytes = { 5, 4, 3, 2, 1 };
BigInteger number = new BigInteger(bytes);
Console.WriteLine("The value of number is {0} (or 0x{0:x}).", number); 
// The example displays the following output: 
//    The value of number is 4328719365 (or 0x102030405).   

The following example instantiates a positive and a negative BigInteger value, passes them to the ToByteArray method, and then restores the original BigInteger values from the resulting byte array. Note that the two values are represented by identical byte arrays. The only difference between them is in the most significant bit of the last element in the byte array. This bit is set (the value of the byte is 0xFF) if the array is created from a negative BigInteger value. The bit is not set (the value of the byte is zero), if the array is created from a positive BigInteger value.

// Instantiate BigInteger values.
BigInteger positiveValue = BigInteger.Parse("4713143110832790377889");
BigInteger negativeValue = BigInteger.Add(-Int64.MaxValue, -60000); 
BigInteger positiveValue2, negativeValue2;

// Create two byte arrays. 
byte[] positiveBytes = positiveValue.ToByteArray();
byte[] negativeBytes = negativeValue.ToByteArray();

// Instantiate new BigInteger from negativeBytes array.
Console.Write("Converted {0:N0} to the byte array ", negativeValue);
foreach (byte byteValue in negativeBytes)
   Console.Write("{0:X2} ", byteValue);
Console.WriteLine();
negativeValue2 = new BigInteger(negativeBytes);
Console.WriteLine("Converted the byte array to {0:N0}", negativeValue2);
Console.WriteLine();

// Instantiate new BigInteger from positiveBytes array.
Console.Write("Converted {0:N0} to the byte array ", positiveValue);
foreach (byte byteValue in positiveBytes)
   Console.Write("{0:X2} ", byteValue);
Console.WriteLine();
positiveValue2 = new BigInteger(positiveBytes);
Console.WriteLine("Converted the byte array to {0:N0}", positiveValue2);
Console.WriteLine();
// The example displays the following output: 
//    Converted -9,223,372,036,854,835,807 to the byte array A1 15 FF FF FF FF FF 7F FF 
//    Converted the byte array to -9,223,372,036,854,835,807 
//     
//    Converted 4,713,143,110,832,790,377,889 to the byte array A1 15 FF FF FF FF FF 7F FF 00 
//    Converted the byte array to 4,713,143,110,832,790,377,889

The following example illustrates how to make sure that a positive value is not incorrectly instantiated as a negative value by adding a byte whose value is zero to the end of the array.

ulong originalNumber = UInt64.MaxValue;
byte[] bytes = BitConverter.GetBytes(originalNumber);
if (originalNumber > 0 && (bytes[bytes.Length - 1] & 0x80) > 0) 
{
   byte[] temp = new byte[bytes.Length];
   Array.Copy(bytes, temp, bytes.Length);
   bytes = new byte[temp.Length + 1];
   Array.Copy(temp, bytes, temp.Length);
}

BigInteger newNumber = new BigInteger(bytes);
Console.WriteLine("Converted the UInt64 value {0:N0} to {1:N0}.", 
                  originalNumber, newNumber); 
// The example displays the following output: 
//    Converted the UInt64 value 18,446,744,073,709,551,615 to 18,446,744,073,709,551,615.

.NET Framework

Supported in: 4.6, 4.5, 4

.NET Framework Client Profile

Supported in: 4

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone 8.1

Windows Phone 8.1, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft