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)>]
new : 
        value:byte[] -> BigInteger

Parameters

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

ExceptionCondition
ArgumentNullException

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

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 a null reference (Nothing in Visual Basic), 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.

No code example is currently available or this language may not be supported.

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.

No code example is currently available or this language may not be supported.

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.

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4

.NET Framework Client Profile

Supported in: 4

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft