BigInteger Constructor (array<Byte[])

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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)

Syntax

'Declaration
<CLSCompliantAttribute(False)> _
Public Sub New ( _
    value As Byte() _
)
[CLSCompliantAttribute(false)]
public BigInteger(
    byte[] value
)

Parameters

  • value
    Type: array<System.Byte[]
    An array of byte values in little-endian order.

Exceptions

Exception Condition
ArgumentNullException

value is nulla null reference (Nothing in Visual Basic).

Remarks

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(array<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 nulla null reference (Nothing in Visual Basic), the constructor throws an ArgumentNullException.

Examples

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.

Dim bytes() As Byte = {5, 4, 3, 2, 1}
Dim number As New BigInteger(bytes)
outputBlock.Text += String.Format("The value of number is {0} (or 0x{0:x}).", number) & vbCrLf
' The example displays the following output:
'    The value of number is 4328719365 (or 0x102030405).   
byte[] bytes = { 5, 4, 3, 2, 1 };
BigInteger number = new BigInteger(bytes);
outputBlock.Text += String.Format("The value of number is {0} (or 0x{0:x}).", number) + "\n";
// 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.
Dim positiveValue As BigInteger = CType(UInt64.MaxValue, BigInteger) * 255 + 9223372036854716064
Dim negativeValue As BigInteger = BigInteger.Add(-Int64.MaxValue, -60000)
Dim positiveValue2, negativeValue2 As BigInteger

' Create two byte arrays.
Dim positiveBytes() As Byte = positiveValue.ToByteArray()
Dim negativeBytes() As Byte = negativeValue.ToByteArray()

' Instantiate new BigInteger from negativeBytes array.
outputBlock.Text += String.Format("Converted {0} to the byte array ", negativeValue)
For Each byteValue As Byte In negativeBytes
   outputBlock.Text += String.Format("{0:X2} ", byteValue)
Next
outputBlock.Text &= vbCrLf
negativeValue2 = New BigInteger(negativeBytes)
outputBlock.Text += String.Format("Converted the byte array to {0}", negativeValue2) & vbCrLf
outputBlock.Text &= vbCrLf

' Instantiate new BigInteger from positiveBytes array.
outputBlock.Text += String.Format("Converted {0} to the byte array ", positiveValue)
For Each byteValue As Byte In positiveBytes
   outputBlock.Text += String.Format("{0:X2} ", byteValue)
Next
outputBlock.Text &= vbCrLf
positiveValue2 = New BigInteger(positiveBytes)
outputBlock.Text += String.Format("Converted the byte array to {0}", positiveValue2) & vbCrLf
outputBlock.Text &= vbCrLf
' The example displays the following output:
'    Converted -9223372036854835807 to the byte array A1 15 FF FF FF FF FF 7F FF
'    Converted the byte array to -9223372036854835807
'    
'    Converted 4713143110832790377889 to the byte array A1 15 FF FF FF FF FF 7F FF 00
'    Converted the byte array to 4713143110832790377889
// Instantiate BigInteger values.
BigInteger positiveValue = ((BigInteger) UInt64.MaxValue) * 255 + 9223372036854716064;
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.

outputBlock.Text += String.Format("Converted {0} to the byte array ", negativeValue);
foreach (byte byteValue in negativeBytes)
   outputBlock.Text += String.Format("{0:X2} ", byteValue);

outputBlock.Text += "\n";
negativeValue2 = new BigInteger(negativeBytes);
outputBlock.Text += String.Format("Converted the byte array to {0}", negativeValue2) + "\n";
outputBlock.Text += "\n";

// Instantiate new BigInteger from positiveBytes array.
outputBlock.Text += String.Format("Converted {0} to the byte array ", positiveValue);
foreach (byte byteValue in positiveBytes)
   outputBlock.Text += String.Format("{0:X2} ", byteValue);

outputBlock.Text += "\n";
positiveValue2 = new BigInteger(positiveBytes);
outputBlock.Text += String.Format("Converted the byte array to {0}", positiveValue2) + "\n";
outputBlock.Text += "\n";
// 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 -9223372036854835807
//    
//    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 4713143110832790377889

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.

Dim originalNumber As ULong = UInt64.MaxValue
' Convert an unsigned integer to a byte array.
Dim bytes() As Byte = BitConverter.GetBytes(originalNumber)
' Determine whether the MSB of the highest-order byte is set.
If originalNumber > 0 And (bytes(bytes.Length - 1) And &H80) > 0 Then
   ' If the MSB is set, add one zero-value byte to the end of the array.
   ReDim Preserve bytes(bytes.Length)
End If

Dim newNumber As New BigInteger(bytes)
outputBlock.Text += String.Format("Converted the UInt64 value {0} to {1}.",  
                  originalNumber, newNumber) + vbCrLf
' The example displays the following output:
'    Converted the UInt64 value 18446744073709551615 to 18446744073709551615.
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);
outputBlock.Text += String.Format("Converted the UInt64 value {0} to {1}.",
                  originalNumber, newNumber) + "\n";
// The example displays the following output:
//    Converted the UInt64 value 18446744073709551615 to 18446744073709551615.

Version Information

Silverlight

Supported in: 5, 4

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.