System.Numerics.BigInteger 構造体

この記事では、この API のリファレンス ドキュメントへの補足的な解説を提供します。

この BigInteger 型は、理論上の値に上限または下限がない任意の大きな整数を表す不変型です。 型のメンバーは、BigInteger他の整数型 (Byte, , Int16, , Int32, Int64UInt16SByteUInt32型) のメンバーとUInt64密接に平行です。 この型は、.NET の他の整数型とは異なり、範囲はプロパティ MinValue によって MaxValue 示されます。

Note

BigInteger型は不変であり (変更可能性を参照)、上限または下限がないため、OutOfMemoryException値が大きくなりすぎる操作に対してスローされる可能性BigIntegerがあります。

BigInteger オブジェクトをインスタンス化する

オブジェクトは BigInteger 、いくつかの方法でインスタンス化できます。

  • キーワード (keyword)をnew使用し、任意の整数または浮動小数点値をコンストラクターのBigIntegerパラメーターとして指定できます。 (浮動小数点値は、割り当てられる前に切り捨てられますBigInteger。)次の例は、キーワード (keyword)を使用して値をnewインスタンス化BigIntegerする方法を示しています。

    BigInteger bigIntFromDouble = new BigInteger(179032.6541);
    Console.WriteLine(bigIntFromDouble);
    BigInteger bigIntFromInt64 = new BigInteger(934157136952);
    Console.WriteLine(bigIntFromInt64);
    // The example displays the following output:
    //   179032
    //   934157136952
    
    Dim bigIntFromDouble As New BigInteger(179032.6541)
    Console.WriteLine(bigIntFromDouble)
    Dim bigIntFromInt64 As New BigInteger(934157136952)
    Console.WriteLine(bigIntFromInt64)
    ' The example displays the following output:
    '   179032
    '   934157136952
    
  • 変数を BigInteger 宣言し、その値が整数型である限り、任意の数値型と同様に値を割り当てることができます。 次の例では、代入を使用して BigIntegerInt64

    long longValue = 6315489358112;
    BigInteger assignedFromLong = longValue;
    Console.WriteLine(assignedFromLong);
    // The example displays the following output:
    //   6315489358112
    
    Dim longValue As Long = 6315489358112
    Dim assignedFromLong As BigInteger = longValue
    Console.WriteLine(assignedFromLong)
    ' The example displays the following output:
    '   6315489358112
    
  • 値をキャストするか、最初に変換する場合は、 BigInteger 10 進値または浮動小数点値をオブジェクトに割り当てることができます。 次の例では、明示的に (C# で) キャストするか、(Visual Basic では) a Double と値を DecimalBigInteger.

    BigInteger assignedFromDouble = (BigInteger) 179032.6541;
    Console.WriteLine(assignedFromDouble);
    BigInteger assignedFromDecimal = (BigInteger) 64312.65m;
    Console.WriteLine(assignedFromDecimal);
    // The example displays the following output:
    //   179032
    //   64312
    
    Dim assignedFromDouble As BigInteger = CType(179032.6541, BigInteger)
    Console.WriteLine(assignedFromDouble)
    Dim assignedFromDecimal As BigInteger = CType(64312.65D, BigInteger)
    Console.WriteLine(assignedFromDecimal)
    ' The example displays the following output:
    '   179032
    '   64312
    

これらのメソッドを使用すると、既存の数値型のいずれか 1 つの範囲内にある値を持つオブジェクトをインスタンス化 BigInteger できます。 既存の数値型の範囲を BigInteger 超える値を持つオブジェクトは、次の 3 つの方法のいずれかでインスタンス化できます。

  • キーワード (keyword)をnew使用し、任意のサイズのバイト配列をコンストラクターにBigInteger.BigInteger提供できます。 次に例を示します。

    byte[] byteArray = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
    BigInteger newBigInt = new BigInteger(byteArray);
    Console.WriteLine("The value of newBigInt is {0} (or 0x{0:x}).", newBigInt);
    // The example displays the following output:
    //   The value of newBigInt is 4759477275222530853130 (or 0x102030405060708090a).
    
    Dim byteArray() As Byte = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}
    Dim newBigInt As New BigInteger(byteArray)
    Console.WriteLine("The value of newBigInt is {0} (or 0x{0:x}).", newBigInt)
    ' The example displays the following output:
    '   The value of newBigInt is 4759477275222530853130 (or 0x102030405060708090a).
    
  • またはTryParseメソッドをParse呼び出して、数値BigIntegerの文字列形式を . 次に例を示します。

    string positiveString = "91389681247993671255432112000000";
    string negativeString = "-90315837410896312071002088037140000";
    BigInteger posBigInt = 0;
    BigInteger negBigInt = 0;
    
    try {
       posBigInt = BigInteger.Parse(positiveString);
       Console.WriteLine(posBigInt);
    }
    catch (FormatException)
    {
       Console.WriteLine("Unable to convert the string '{0}' to a BigInteger value.",
                         positiveString);
    }
    
    if (BigInteger.TryParse(negativeString, out negBigInt))
      Console.WriteLine(negBigInt);
    else
       Console.WriteLine("Unable to convert the string '{0}' to a BigInteger value.",
                          negativeString);
    
    // The example displays the following output:
    //   9.1389681247993671255432112E+31
    //   -9.0315837410896312071002088037E+34
    
    Dim positiveString As String = "91389681247993671255432112000000"
    Dim negativeString As String = "-90315837410896312071002088037140000"
    Dim posBigInt As BigInteger = 0
    Dim negBigInt As BigInteger = 0
    
    Try
        posBigInt = BigInteger.Parse(positiveString)
        Console.WriteLine(posBigInt)
    Catch e As FormatException
        Console.WriteLine("Unable to convert the string '{0}' to a BigInteger value.",
                          positiveString)
    End Try
    
    If BigInteger.TryParse(negativeString, negBigInt) Then
        Console.WriteLine(negBigInt)
    Else
        Console.WriteLine("Unable to convert the string '{0}' to a BigInteger value.",
                           negativeString)
    End If
    ' The example displays the following output:
    '   9.1389681247993671255432112E+31
    '   -9.0315837410896312071002088037E+34
    
  • 数式に対して何らかの操作をstatic実行し、計算結果BigIntegerを返す (SharedVisual Basic では) BigInteger メソッドを呼び出すことができます。 次の例では、結果BigIntegerをキューにUInt64.MaxValue割り当てて結果を .

    BigInteger number = BigInteger.Pow(UInt64.MaxValue, 3);
    Console.WriteLine(number);
    // The example displays the following output:
    //    6277101735386680762814942322444851025767571854389858533375
    
    Dim number As BigInteger = BigInteger.Pow(UInt64.MaxValue, 3)
    Console.WriteLine(number)
    ' The example displays the following output:
    ' 6277101735386680762814942322444851025767571854389858533375
    

a BigInteger の初期化されていない値は Zero.

BigInteger 値に対して操作を実行する

他の整数型を BigInteger 使用する場合と同様に、インスタンスを使用できます。 BigInteger では、加算、減算、除算、乗算、単項否定などの基本的な算術演算を実行できるように、標準の数値演算子がオーバーロードされます。 また、標準の数値演算子を使用して、2 つの BigInteger 値を相互に比較することもできます。 他の整数型と同様に、ビット演算子AndBigIntegerOrXOr左シフト演算子、左シフト演算子、右シフト演算子もサポートします。 カスタム演算子をサポートしていない言語の場合、構造体 BigInteger には数学演算を実行するための同等のメソッドも用意されています。 これには、、Divide、、MultiplyNegate、、Subtractその他のいくつかが含Addまれます。

構造体の多くのメンバーは、 BigInteger 他の整数型のメンバーに直接対応します。 さらに、 BigInteger 次のようなメンバーを追加します。

これらの追加メンバーの多くは、プリミティブ数値型を Math 操作する機能を提供するクラスのメンバーに対応しています。

変更可能性

次の例では、オブジェクトを BigInteger インスタンス化し、その値を 1 ずつインクリメントします。

BigInteger number = BigInteger.Multiply(Int64.MaxValue, 3);
number++;
Console.WriteLine(number);
Dim number As BigInteger = BigInteger.Multiply(Int64.MaxValue, 3)
number += 1
Console.WriteLine(number)

この例では既存のオブジェクトの値を変更しているように見えますが、これは当てはまるわけではありません。 BigInteger オブジェクトは不変です。つまり、内部的には、共通言語ランタイムは実際に新しい BigInteger オブジェクトを作成し、以前の値より 1 大きい値を割り当てます。 その後、この新しいオブジェクトが呼び出し元に返されます。

Note

.NET の他の数値型も変更できません。 ただし、型には BigInteger 上限または下限がないため、値が非常に大きくなり、パフォーマンスに測定可能な影響を与える可能性があります。

このプロセスは呼び出し元には透過的ですが、パフォーマンスの低下が発生します。 場合によっては、特に非常に大きな BigInteger 値に対して繰り返し操作がループで実行される場合、パフォーマンスの低下が大きくなる可能性があります。 たとえば、次の例では、100 万回まで操作が繰り返し実行され BigInteger 、操作が成功するたびに値が 1 ずつインクリメントされます。

BigInteger number = Int64.MaxValue ^ 5;
int repetitions = 1000000;
// Perform some repetitive operation 1 million times.
for (int ctr = 0; ctr <= repetitions; ctr++)
{
    // Perform some operation. If it fails, exit the loop.
    if (!SomeOperationSucceeds()) break;
    // The following code executes if the operation succeeds.
    number++;
}
Dim number As BigInteger = Int64.MaxValue ^ 5
Dim repetitions As Integer = 1000000
' Perform some repetitive operation 1 million times.
For ctr As Integer = 0 To repetitions
    ' Perform some operation. If it fails, exit the loop.
    If Not SomeOperationSucceeds() Then Exit For
    ' The following code executes if the operation succeeds.
    number += 1
Next

このような場合は、変数に対するすべての中間代入を実行することで、パフォーマンスを Int32 向上させることができます。 その後、ループが終了したときに、変数の最終的な値を BigInteger オブジェクトに割り当てることができます。 具体的な例を次に示します。

BigInteger number = Int64.MaxValue ^ 5;
int repetitions = 1000000;
int actualRepetitions = 0;
// Perform some repetitive operation 1 million times.
for (int ctr = 0; ctr <= repetitions; ctr++)
{
    // Perform some operation. If it fails, exit the loop.
    if (!SomeOperationSucceeds()) break;
    // The following code executes if the operation succeeds.
    actualRepetitions++;
}
number += actualRepetitions;
Dim number As BigInteger = Int64.MaxValue ^ 5
Dim repetitions As Integer = 1000000
Dim actualRepetitions As Integer = 0
' Perform some repetitive operation 1 million times.
For ctr As Integer = 0 To repetitions
    ' Perform some operation. If it fails, exit the loop.
    If Not SomeOperationSucceeds() Then Exit For
    ' The following code executes if the operation succeeds.
    actualRepetitions += 1
Next
number += actualRepetitions

バイト配列と 16 進文字列

値をバイト配列に変換 BigInteger する場合、またはバイト配列を値に変換する BigInteger 場合は、バイトの順序を考慮する必要があります。 この構造体では BigInteger 、バイト配列内の個々のバイトがリトル エンディアン順 (つまり、値の下位バイトの前の上位バイト) で表示されることを想定しています。 次の例に示すように、メソッドをBigIntegerToByteArray呼び出し、結果のバイト配列をコンストラクターにBigInteger(Byte[])渡すことで、値をラウンドトリップできます。

BigInteger number = BigInteger.Pow(Int64.MaxValue, 2);
Console.WriteLine(number);

// Write the BigInteger value to a byte array.
byte[] bytes = number.ToByteArray();

// Display the byte array.
foreach (byte byteValue in bytes)
    Console.Write("0x{0:X2} ", byteValue);
Console.WriteLine();

// Restore the BigInteger value from a Byte array.
BigInteger newNumber = new BigInteger(bytes);
Console.WriteLine(newNumber);
// The example displays the following output:
//    8.5070591730234615847396907784E+37
//    0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0x3F
//
//    8.5070591730234615847396907784E+37
Dim number As BigInteger = BigInteger.Pow(Int64.MaxValue, 2)     
Console.WriteLine(number)

' Write the BigInteger value to a byte array.
Dim bytes() As Byte = number.ToByteArray()

' Display the byte array.
For Each byteValue As Byte In bytes
   Console.Write("0x{0:X2} ", byteValue)
Next   
Console.WriteLine()

' Restore the BigInteger value from a Byte array.
Dim newNumber As BigInteger = New BigInteger(bytes)
Console.WriteLine(newNumber)               
' The example displays the following output:
'    8.5070591730234615847396907784E+37
'    0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0x3F
'    
'    8.5070591730234615847396907784E+37

他の整数型の値を表すバイト配列から値をインスタンス化 BigInteger するには、整数値をメソッドに BitConverter.GetBytes 渡し、結果のバイト配列をコンストラクターに BigInteger(Byte[]) 渡します。 次の例では、値を BigInteger 表すバイト配列から値を Int16 インスタンス化します。

short originalValue = 30000;
Console.WriteLine(originalValue);

// Convert the Int16 value to a byte array.
byte[] bytes = BitConverter.GetBytes(originalValue);

// Display the byte array.
foreach (byte byteValue in bytes)
    Console.Write("0x{0} ", byteValue.ToString("X2"));
Console.WriteLine();

// Pass byte array to the BigInteger constructor.
BigInteger number = new BigInteger(bytes);
Console.WriteLine(number);
// The example displays the following output:
//       30000
//       0x30 0x75
//       30000
Dim originalValue As Short = 30000
Console.WriteLine(originalValue)

' Convert the Int16 value to a byte array.
Dim bytes() As Byte = BitConverter.GetBytes(originalValue)

' Display the byte array.
For Each byteValue As Byte In bytes
   Console.Write("0x{0} ", byteValue.ToString("X2"))
Next    
Console.WriteLine() 

' Pass byte array to the BigInteger constructor.
Dim number As BigInteger = New BigInteger(bytes)
Console.WriteLine(number)
' The example displays the following output:
'       30000
'       0x30 0x75
'       30000

この構造体は BigInteger 、負の値が 2 の補数表現を使用して格納されることを前提としています。 構造体は BigInteger 固定長のない数値を表すので、 BigInteger(Byte[]) コンストラクターは常に配列内の最後のバイトの最上位ビットを符号ビットとして解釈します。 コンストラクターが BigInteger(Byte[]) 負の値の 2 つの補数表現を正の値の符号と大きさ表現と混同しないようにするには、通常、バイト配列の最後のバイトの最上位ビットが設定される正の値に、値が 0 の追加バイトを含める必要があります。 たとえば、0xC0 0xBD 0xF0 0xFFは、-1,000,000 または 4,293,967,296 のいずれかのリトル エンディアン 16 進数表現です。 この配列の最後のバイトの最上位ビットがオンであるため、バイト配列の値はコンストラクターによって BigInteger(Byte[]) -1,000,000 として解釈されます。 正の値を持つ値を BigInteger インスタンス化するには、要素が0xC0 0xBD 0xF0 0xFF 0x00バイト配列をコンストラクターに渡す必要があります。 次の例は、これを示しています。

int negativeNumber = -1000000;
uint positiveNumber = 4293967296;

byte[] negativeBytes = BitConverter.GetBytes(negativeNumber);
BigInteger negativeBigInt = new BigInteger(negativeBytes);
Console.WriteLine(negativeBigInt.ToString("N0"));

byte[] tempPosBytes = BitConverter.GetBytes(positiveNumber);
byte[] positiveBytes = new byte[tempPosBytes.Length + 1];
Array.Copy(tempPosBytes, positiveBytes, tempPosBytes.Length);
BigInteger positiveBigInt = new BigInteger(positiveBytes);
Console.WriteLine(positiveBigInt.ToString("N0"));
// The example displays the following output:
//    -1,000,000
//    4,293,967,296
Dim negativeNumber As Integer = -1000000
Dim positiveNumber As UInteger = 4293967296

Dim negativeBytes() As Byte = BitConverter.GetBytes(negativeNumber) 
Dim negativeBigInt As New BigInteger(negativeBytes)
Console.WriteLine(negativeBigInt.ToString("N0"))

Dim tempPosBytes() As Byte = BitConverter.GetBytes(positiveNumber)
Dim positiveBytes(tempposBytes.Length) As Byte
Array.Copy(tempPosBytes, positiveBytes, tempPosBytes.Length)
Dim positiveBigInt As New BigInteger(positiveBytes)
Console.WriteLine(positiveBigInt.ToString("N0")) 
' The example displays the following output:
'    -1,000,000
'    4,293,967,296

正の値からメソッドによって ToByteArray 作成されたバイト配列には、この余分なゼロ値バイトが含まれます。 したがって、次の BigInteger 例に示すように、構造体は値をバイト配列に割り当ててから復元することで、値を正常にラウンドトリップできます。

BigInteger positiveValue = 15777216;
BigInteger negativeValue = -1000000;

Console.WriteLine("Positive value: " + positiveValue.ToString("N0"));
byte[] bytes = positiveValue.ToByteArray();

foreach (byte byteValue in bytes)
    Console.Write("{0:X2} ", byteValue);
Console.WriteLine();
positiveValue = new BigInteger(bytes);
Console.WriteLine("Restored positive value: " + positiveValue.ToString("N0"));

Console.WriteLine();

Console.WriteLine("Negative value: " + negativeValue.ToString("N0"));
bytes = negativeValue.ToByteArray();
foreach (byte byteValue in bytes)
    Console.Write("{0:X2} ", byteValue);
Console.WriteLine();
negativeValue = new BigInteger(bytes);
Console.WriteLine("Restored negative value: " + negativeValue.ToString("N0"));
// The example displays the following output:
//       Positive value: 15,777,216
//       C0 BD F0 00
//       Restored positive value: 15,777,216
//
//       Negative value: -1,000,000
//       C0 BD F0
//       Restored negative value: -1,000,000
Dim positiveValue As BigInteger = 15777216
Dim negativeValue As BigInteger = -1000000

Console.WriteLine("Positive value: " + positiveValue.ToString("N0"))
Dim bytes() As Byte = positiveValue.ToByteArray()
For Each byteValue As Byte In bytes
   Console.Write("{0:X2} ", byteValue)
Next
Console.WriteLine()
positiveValue = New BigInteger(bytes)
Console.WriteLine("Restored positive value: " + positiveValue.ToString("N0"))

Console.WriteLine()
   
Console.WriteLIne("Negative value: " + negativeValue.ToString("N0"))
bytes = negativeValue.ToByteArray()
For Each byteValue As Byte In bytes
   Console.Write("{0:X2} ", byteValue)
Next
Console.WriteLine()
negativeValue = New BigInteger(bytes)
Console.WriteLine("Restored negative value: " + negativeValue.ToString("N0"))
' The example displays the following output:
'       Positive value: 15,777,216
'       C0 BD F0 00
'       Restored positive value: 15,777,216
'       
'       Negative value: -1,000,000
'       C0 BD F0
'       Restored negative value: -1,000,000

ただし、開発者によって動的に作成されるバイト配列、または符号なし整数をバイト配列に変換するメソッドによって返されるバイト配列に、この追加の 0 値バイトを追加する必要がある場合があります (例: BitConverter.GetBytes(UInt16), BitConverter.GetBytes(UInt32),、)。BitConverter.GetBytes(UInt64)

16 進数の文字列を解析する場合、およびBigInteger.Parse(String, NumberStyles, IFormatProvider)メソッドは、BigInteger.Parse(String, NumberStyles)文字列の最初のバイトの最上位ビットが設定されている場合、または文字列の最初の 16 進数がバイト値の下位 4 ビットを表す場合、値は 2 の補数表現を使用して表されることを前提としています。 たとえば、"FF01" と "F01" の両方が 10 進値 -255 を表します。 正の値と負の値を区別するには、正の値に先頭に 0 を含める必要があります。 メソッドの ToString 関連するオーバーロードは、"X" 書式指定文字列が渡されるときに、正の値に対して返される 16 進文字列に先頭に 0 を追加します。 これにより、次の例に示すように、and Parse メソッドをToString使用して値をラウンドトリップBigIntegerできます。

BigInteger negativeNumber = -1000000;
BigInteger positiveNumber = 15777216;

string negativeHex = negativeNumber.ToString("X");
string positiveHex = positiveNumber.ToString("X");

BigInteger negativeNumber2, positiveNumber2;
negativeNumber2 = BigInteger.Parse(negativeHex,
                                   NumberStyles.HexNumber);
positiveNumber2 = BigInteger.Parse(positiveHex,
                                   NumberStyles.HexNumber);

Console.WriteLine("Converted {0:N0} to {1} back to {2:N0}.",
                   negativeNumber, negativeHex, negativeNumber2);
Console.WriteLine("Converted {0:N0} to {1} back to {2:N0}.",
                   positiveNumber, positiveHex, positiveNumber2);
// The example displays the following output:
//       Converted -1,000,000 to F0BDC0 back to -1,000,000.
//       Converted 15,777,216 to 0F0BDC0 back to 15,777,216.
Dim negativeNumber As BigInteger = -1000000
Dim positiveNumber As BigInteger = 15777216

Dim negativeHex As String = negativeNumber.ToString("X")
Dim positiveHex As string = positiveNumber.ToString("X")

Dim negativeNumber2, positiveNumber2 As BigInteger 
negativeNumber2 = BigInteger.Parse(negativeHex, 
                                   NumberStyles.HexNumber)
positiveNumber2 = BigInteger.Parse(positiveHex,
                                   NumberStyles.HexNumber)

Console.WriteLine("Converted {0:N0} to {1} back to {2:N0}.", 
                   negativeNumber, negativeHex, negativeNumber2)                                         
Console.WriteLine("Converted {0:N0} to {1} back to {2:N0}.", 
                   positiveNumber, positiveHex, positiveNumber2)                                         
' The example displays the following output:
'       Converted -1,000,000 to F0BDC0 back to -1,000,000.
'       Converted 15,777,216 to 0F0BDC0 back to 15,777,216.

ただし、他の整数型のメソッドまたはパラメーターを含むtoBaseメソッドのToStringオーバーロードを呼び出ToStringして作成された 16 進文字列は、16 進文字列の派生元の値またはソース データ型の符号を示すものではありません。 このような文字列から値を BigInteger 正常にインスタンス化するには、追加のロジックが必要です。 次の例では、1 つの可能な実装を示します。

using System;
using System.Globalization;
using System.Numerics;

public struct HexValue
{
    public int Sign;
    public string Value;
}

public class ByteHexExample2
{
    public static void Main()
    {
        uint positiveNumber = 4039543321;
        int negativeNumber = -255423975;

        // Convert the numbers to hex strings.
        HexValue hexValue1, hexValue2;
        hexValue1.Value = positiveNumber.ToString("X");
        hexValue1.Sign = Math.Sign(positiveNumber);

        hexValue2.Value = Convert.ToString(negativeNumber, 16);
        hexValue2.Sign = Math.Sign(negativeNumber);

        // Round-trip the hexadecimal values to BigInteger values.
        string hexString;
        BigInteger positiveBigInt, negativeBigInt;

        hexString = (hexValue1.Sign == 1 ? "0" : "") + hexValue1.Value;
        positiveBigInt = BigInteger.Parse(hexString, NumberStyles.HexNumber);
        Console.WriteLine("Converted {0} to {1} and back to {2}.",
                          positiveNumber, hexValue1.Value, positiveBigInt);

        hexString = (hexValue2.Sign == 1 ? "0" : "") + hexValue2.Value;
        negativeBigInt = BigInteger.Parse(hexString, NumberStyles.HexNumber);
        Console.WriteLine("Converted {0} to {1} and back to {2}.",
                          negativeNumber, hexValue2.Value, negativeBigInt);
    }
}
// The example displays the following output:
//       Converted 4039543321 to F0C68A19 and back to 4039543321.
//       Converted -255423975 to f0c68a19 and back to -255423975.
Imports System.Globalization
Imports System.Numerics

Public Structure HexValue
    Public Sign As Integer
    Public Value As String
End Structure

Module Example2
    Public Sub Main()
        Dim positiveNumber As UInteger = 4039543321
        Dim negativeNumber As Integer = -255423975

        ' Convert the numbers to hex strings.
        Dim hexValue1, hexValue2 As HexValue
        hexValue1.Value = positiveNumber.ToString("X")
        hexValue1.Sign = Math.Sign(positiveNumber)

        hexValue2.Value = Convert.ToString(negativeNumber, 16)
        hexValue2.Sign = Math.Sign(negativeNumber)

        ' Round-trip the hexadecimal values to BigInteger values.
        Dim hexString As String
        Dim positiveBigInt, negativeBigInt As BigInteger

        hexString = CStr(IIf(hexValue1.Sign = 1, "0", "")) + hexValue1.Value
        positiveBigInt = BigInteger.Parse(hexString, NumberStyles.HexNumber)
        Console.WriteLine("Converted {0} to {1} and back to {2}.",
                        positiveNumber, hexValue1.Value, positiveBigInt)

        hexString = CStr(IIf(hexValue2.Sign = 1, "0", "")) + hexValue2.Value
        negativeBigInt = BigInteger.Parse(hexString, NumberStyles.HexNumber)
        Console.WriteLine("Converted {0} to {1} and back to {2}.",
                        negativeNumber, hexValue2.Value, negativeBigInt)

    End Sub
End Module
' The example displays the following output:
'       Converted 4039543321 to F0C68A19 and back to 4039543321.
'       Converted -255423975 to f0c68a19 and back to -255423975.