Struct System.Numerics.BigInteger

Artikel ini menyediakan keterangan tambahan untuk dokumentasi referensi untuk API ini.

Jenisnya BigInteger adalah jenis yang tidak dapat diubah yang mewakili bilangan bulat besar yang nilainya dalam teori tidak memiliki batas atas atau bawah. Anggota jenis BigInteger sejajar dengan jenis integral lainnya (Bytejenis , , Int16, Int64SByteInt32, UInt16, , UInt32dan ).UInt64 Jenis ini berbeda dari jenis integral lainnya di .NET, yang memiliki rentang yang ditunjukkan oleh properti dannya MinValueMaxValue .

Catatan

BigInteger Karena jenisnya tidak dapat diubah (lihat Mutabilitas) dan karena tidak memiliki batas atas atau bawah, dapat OutOfMemoryException dilemparkan untuk operasi apa pun yang menyebabkan BigInteger nilai tumbuh terlalu besar.

Membuat instans objek BigInteger

Anda dapat membuat BigInteger instans objek dengan beberapa cara:

  • Anda dapat menggunakan new kata kunci dan memberikan nilai integral atau floating-point sebagai parameter ke BigInteger konstruktor. (Nilai floating-point dipotong sebelum ditetapkan ke BigInteger.) Contoh berikut mengilustrasikan cara menggunakan new kata kunci untuk membuat BigInteger instans nilai.

    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
    
  • Anda dapat mendeklarasikan BigInteger variabel dan menetapkannya nilai sama seperti jenis numerik apa pun, selama nilai tersebut adalah jenis integral. Contoh berikut menggunakan penugasan untuk membuat BigInteger nilai dari Int64.

    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
    
  • Anda dapat menetapkan nilai desimal atau floating-point ke BigInteger objek jika Anda mentransmisikan nilai atau mengonversinya terlebih dahulu. Contoh berikut secara eksplisit melemparkan (dalam C#) atau mengonversi (di Visual Basic) dan DoubleDecimal nilai menjadi BigInteger.

    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
    

Metode ini memungkinkan Anda membuat instans BigInteger objek yang nilainya berada dalam rentang salah satu jenis numerik yang ada saja. Anda dapat membuat BigInteger instans objek yang nilainya dapat melebihi rentang jenis numerik yang ada dengan salah satu dari tiga cara:

  • Anda dapat menggunakan new kata kunci dan menyediakan array byte dengan ukuran apa pun ke BigInteger.BigInteger konstruktor. Misalnya:

    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).
    
  • Anda dapat memanggil Parse metode atau TryParse untuk mengonversi representasi string dari angka menjadi BigInteger. Misalnya:

    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
    
  • Anda dapat memanggil static metode (Shared dalam Visual Basic) BigInteger yang melakukan beberapa operasi pada ekspresi numerik dan mengembalikan hasil terhitung BigInteger . Contoh berikut melakukan ini dengan menyisir UInt64.MaxValue dan menetapkan hasilnya ke BigInteger.

    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
    

Nilai yang BigInteger tidak diinisialisasi adalah Zero.

Melakukan operasi pada nilai BigInteger

Anda dapat menggunakan BigInteger instans karena Anda akan menggunakan jenis integral lainnya. BigInteger membebani operator numerik standar untuk memungkinkan Anda melakukan operasi matematika dasar seperti penambahan, pengurangan, pembagian, perkalian, dan negasi unary. Anda juga dapat menggunakan operator numerik standar untuk membandingkan dua BigInteger nilai satu sama lain. Seperti jenis integral lainnya, BigInteger juga mendukung operator bitwise And, , OrXOr, shift kiri, dan shift kanan. Untuk bahasa yang tidak mendukung operator kustom, BigInteger struktur juga menyediakan metode yang setara untuk melakukan operasi matematika. Ini termasuk Add, , MultiplyDivide, Negate, Subtract, dan beberapa lainnya.

Banyak anggota BigInteger struktur sesuai langsung dengan anggota jenis integral lainnya. Selain itu, BigInteger menambahkan anggota seperti berikut ini:

Banyak dari anggota tambahan ini sesuai dengan anggota Math kelas, yang menyediakan fungsionalitas untuk bekerja dengan jenis numerik primitif.

Mutabilitas

Contoh berikut membuat instans BigInteger objek lalu menaikkan nilainya satu per satu.

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)

Meskipun contoh ini tampaknya mengubah nilai objek yang ada, ini bukan masalahnya. BigInteger objek tidak dapat diubah, yang berarti bahwa secara internal, runtime bahasa umum benar-benar membuat objek baru BigInteger dan menetapkan nilai satu lebih besar dari nilai sebelumnya. Objek baru ini kemudian dikembalikan ke pemanggil.

Catatan

Jenis numerik lainnya di .NET juga tidak dapat diubah. Namun, karena jenisnya BigInteger tidak memiliki batas atas atau bawah, nilainya dapat tumbuh sangat besar dan memiliki dampak terukur pada performa.

Meskipun proses ini transparan untuk pemanggil, proses ini dikenakan penalti performa. Dalam beberapa kasus, terutama ketika operasi berulang dilakukan dalam perulangan pada nilai yang sangat besar BigInteger , bahwa penalti performa bisa signifikan. Misalnya, dalam contoh berikut, operasi dilakukan berulang hingga satu juta kali, dan BigInteger nilai bertambah satu kali setiap kali operasi berhasil.

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

Dalam kasus seperti itu, Anda dapat meningkatkan performa dengan melakukan semua penugasan perantara ke Int32 variabel. Nilai akhir variabel kemudian dapat ditetapkan ke BigInteger objek ketika perulangan keluar. Contoh berikut memberikan ilustrasi.

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

Array byte dan string heksadesimal

Jika Anda mengonversi BigInteger nilai menjadi array byte, atau jika Anda mengonversi array byte menjadi BigInteger nilai, Anda harus mempertimbangkan urutan byte. Struktur BigInteger mengharapkan byte individual dalam array byte muncul dalam urutan little-endian (yaitu, byte urutan yang lebih rendah dari nilai mendahului byte urutan yang lebih tinggi). Anda dapat melakukan round-trip BigInteger nilai dengan memanggil ToByteArray metode lalu meneruskan array byte yang dihasilkan ke BigInteger(Byte[]) konstruktor, seperti yang ditunjukkan contoh berikut.

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

Untuk membuat instans BigInteger nilai dari array byte yang mewakili nilai dari beberapa jenis integral lainnya, Anda dapat meneruskan nilai integral ke BitConverter.GetBytes metode , lalu meneruskan array byte yang dihasilkan ke BigInteger(Byte[]) konstruktor. Contoh berikut membuat instans BigInteger nilai dari array byte yang mewakili Int16 nilai.

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

Struktur BigInteger mengasumsikan bahwa nilai negatif disimpan dengan menggunakan representasi pelengkap dua. BigInteger Karena struktur mewakili nilai numerik tanpa panjang tetap, BigInteger(Byte[]) konstruktor selalu menginterpretasikan bit paling signifikan dari byte terakhir dalam array sebagai bit tanda. Untuk mencegah BigInteger(Byte[]) konstruktor membingungkan representasi pelengkap keduanya dari nilai negatif dengan representasi tanda dan besarnya nilai positif, nilai positif di mana bit paling signifikan dari byte terakhir dalam array byte biasanya akan ditetapkan harus menyertakan byte tambahan yang nilainya adalah 0. Misalnya, 0xC0 0xBD 0xF0 0xFF adalah representasi heksadesimal little-endian dari -1.000.000 atau 4.293.967.296. Karena bit yang paling signifikan dari byte terakhir dalam array ini aktif, nilai array byte akan ditafsirkan oleh BigInteger(Byte[]) konstruktor sebagai -1.000.000. Untuk membuat instans nilainya BigInteger positif, array byte yang elemennya 0xC0 0xBD 0xF0 0xFF 0x00 harus diteruskan ke konstruktor. Contoh berikut mengilustrasikan langkah-langkah ini:

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

Array byte yang dibuat oleh ToByteArray metode dari nilai positif menyertakan byte nilai nol tambahan ini. Oleh karena itu, BigInteger struktur dapat berhasil melakukan perjalanan pulang pergi dengan menetapkannya, lalu memulihkannya dari, array byte, seperti yang ditunjukkan contoh berikut.

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

Namun, Anda mungkin perlu menambahkan byte nilai nol tambahan ini ke array byte yang dibuat secara dinamis oleh pengembang atau yang dikembalikan oleh metode yang mengonversi bilangan bulat yang tidak ditandatangani ke array byte (seperti BitConverter.GetBytes(UInt16), , BitConverter.GetBytes(UInt32)dan BitConverter.GetBytes(UInt64)).

Saat mengurai string heksadesimal, BigInteger.Parse(String, NumberStyles) metode dan BigInteger.Parse(String, NumberStyles, IFormatProvider) mengasumsikan bahwa jika bit yang paling signifikan dari byte pertama dalam string diatur, atau jika digit heksadesimal pertama dari string mewakili empat bit yang lebih rendah dari nilai byte, nilai diwakili dengan menggunakan representasi pelengkap dua. Misalnya, "FF01" dan "F01" mewakili nilai desimal -255. Untuk membedakan positif dari nilai negatif, nilai positif harus menyertakan nol di depannya. Kelebihan beban ToString metode yang relevan, ketika mereka melewati string format "X", tambahkan nol di depan ke string heksadesimal yang dikembalikan untuk nilai positif. Ini memungkinkan untuk melakukan perjalanan BigInteger pulang pergi dengan menggunakan ToString metode dan Parse , seperti yang ditunjukkan contoh berikut.

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.

Namun, string heksadesimal yang dibuat dengan memanggil ToString metode jenis integral lainnya atau kelebihan beban ToString metode yang menyertakan toBase parameter tidak menunjukkan tanda nilai atau jenis data sumber tempat string heksadesimal diturunkan. Berhasil membuat instans BigInteger nilai dari string seperti itu memerlukan beberapa logika tambahan. Contoh berikut memberikan satu kemungkinan implementasi.

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.