Поделиться через


System.Numerics.BigInteger struct

В этой статье приводятся дополнительные замечания к справочной документации по этому API.

Тип BigInteger — это неизменяемый тип, представляющий произвольно большое целое число, значение которого в теории не имеет верхних или нижних границ. Члены типа BigInteger тесно параллельны другим целочисленным типам (Byte, Int16, Int32, Int64, SByte, UInt16, UInt32и UInt64). Этот тип отличается от других целочисленных типов в .NET, которые имеют диапазон, указанный свойствами MinValue и MaxValue.

Примечание.

Так как тип BigInteger неизменяем (см. Мутируемость) и поскольку он не имеет верхних или нижних границ, OutOfMemoryException можно выбросить при любой операции, которая приводит к тому, что значение BigInteger становится слишком большим.

Создание экземпляра объекта BigInteger

Создать экземпляр объекта BigInteger можно несколькими способами:

  • Ключевое слово new можно использовать и предоставить любое целочисленное или плавающее значение в качестве параметра конструктору BigInteger. (Значения с плавающей запятой усекаются перед присвоением BigInteger.) В следующем примере показано, как использовать ключевое слово 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 и назначить ее значение так же, как и любой числовой тип, если это значение является целочисленным типом. В следующем примере используется присваивание для создания значения BigInteger из 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
    
  • Десятичное или плавающее значение можно назначить объекту BigInteger, если сначала присвоить значение или преобразовать его. В следующем примере явно выполняется приведение (в C#) или преобразование (в Visual Basic) значения Double и значения Decimal в 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
    

Эти методы позволяют создать экземпляр объекта BigInteger, значение которого находится только в диапазоне одного из существующих числовых типов. Можно создать экземпляр объекта BigInteger, значение которого может превышать диапазон существующих числовых типов одним из трех способов:

  • Ключевое слово 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 {newBigInt} (or 0x{newBigInt:x}).");
    // 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).
    
  • Можно вызвать методы Parse или TryParse, чтобы преобразовать строковое представление числа в 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 '{positiveString}' to a BigInteger value.");
    }
    
    if (BigInteger.TryParse(negativeString, out negBigInt))
      Console.WriteLine(negBigInt);
    else
       Console.WriteLine($"Unable to convert the string '{negativeString}' to a BigInteger value.");
    
    // 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 (Shared в Visual Basic BigInteger), который выполняет некоторую операцию с числовым выражением и возвращает вычисляемый BigInteger результат. В следующем примере выполняется возведение UInt64.MaxValue в куб и присваивание результата 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
    

Неинициализированное значение BigInteger равно Zero.

Выполнение операций со значениями BigInteger

Вы можете использовать экземпляр BigInteger так же, как и любой другой тип целого числа. BigInteger перегружает стандартные числовые операторы, чтобы обеспечить выполнение основных математических операций, таких как добавление, вычитание, деление, умножение и унарное отрицание. Можно также использовать стандартные числовые операторы для сравнения двух BigInteger значений друг с другом. Как и другие целочисленные типы, BigInteger также поддерживает побитовые And, Or, XOr, сдвиг влево и операторы сдвига вправо. Для языков, которые не поддерживают пользовательские операторы, структура BigInteger также предоставляет эквивалентные методы для выполнения математических операций. К ним относятся Add, Divide, Multiply, Negate, Subtractи несколько других.

Многие члены структуры BigInteger соответствуют непосредственно членам других целочисленных типов. Кроме того, BigInteger добавляет элементы, такие как:

  • Sign, который возвращает значение для определения знака BigInteger.

  • Abs, который возвращает абсолютное значение значения BigInteger.

  • DivRem, который возвращает как кворот, так и оставшуюся часть операции деления.

  • GreatestCommonDivisor, который возвращает наибольший общий делитель двух BigInteger значений.

Многие из этих дополнительных элементов соответствуют членам класса Math, который предоставляет функциональные возможности для работы с примитивными числовыми типами.

Изменяемость

В следующем примере создается экземпляр объекта BigInteger, а затем увеличивает его значение на один.

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 объекты неизменяемы, что означает, что внутренняя среда CLR фактически создает новый объект BigInteger и назначает его значение больше предыдущего значения. Затем этот новый объект возвращается вызывающему.

Примечание.

Другие числовые типы в .NET также неизменяемы. Тем не менее, поскольку тип BigInteger не имеет верхних или нижних границ, его значения могут увеличиваться чрезвычайно большими и иметь измеримое влияние на производительность.

Хотя этот процесс является прозрачным для вызывающего, он приводит к снижению производительности. В некоторых случаях, особенно если повторяющиеся операции выполняются в цикле с очень большими значениями BigInteger, штраф за производительность может быть значительным. Например, в следующем примере операция выполняется повторно до миллиона раз, а значение BigInteger увеличивается по одному разу при успешном выполнении операции.

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

Массивы байтов и шестнадцатеричные строки

При преобразовании BigInteger значений в массивы байтов или при преобразовании массивов байтов в BigInteger значения необходимо учитывать порядок байтов. Структура BigInteger ожидает, что отдельные байты в массиве байтов будут отображаться в маленьком порядке (т. е. байты нижнего порядка значения предшествуют байтам более высокого порядка). Можно обойти значение BigInteger путем вызова метода ToByteArray, а затем передачи результирующего массива байтов в конструктор 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 предполагает, что отрицательные значения хранятся с помощью двух дополнительных представлений. Так как структура BigInteger представляет числовое значение без фиксированной длины, конструктор BigInteger(Byte[]) всегда интерпретирует наиболее значительный бит последнего байта в массиве как бит знака. Чтобы BigInteger(Byte[]) конструктор не путал двоичное дополнение для представления отрицательного значения с представлением со знаком и величиной для положительного значения, положительные значения, в которых наиболее значимый бит последнего байта в массиве байтов обычно устанавливается, должны включать дополнительный байт со значением 0. Например, 0xC0 0xBD 0xF0 0xFF является шестнадцатеричным представлением числа -1 000 000 или 4 293 967 296. Так как самый значительный бит последнего байта в этом массиве включен, значение массива байтов будет интерпретировано конструктором BigInteger(Byte[]) как -1000 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

Однако может потребоваться добавить этот дополнительный байт нулевого значения в массивы байтов, создаваемые динамически разработчиком или возвращаемые методами, которые преобразуют целые числа без знака в массивы байтов (например, BitConverter.GetBytes(UInt16), BitConverter.GetBytes(UInt32)и BitConverter.GetBytes(UInt64)).

При синтаксическом анализе шестнадцатеричной строки методы BigInteger.Parse(String, NumberStyles) и BigInteger.Parse(String, NumberStyles, IFormatProvider) предполагают, что если задан самый значительный бит первого байта в строке, или если первая шестнадцатеричная цифра строки представляет нижние четыре бита байта, значение представлено с помощью двух дополнительных представлений. Например, как FF01, так и F01 представляют десятичное значение -255. Чтобы отличить положительные от отрицательных значений, положительные значения должны содержать начальный ноль. Соответствующие перегрузки метода ToString, когда им передается строка формата "X", добавляют начальный ноль в возвращаемую шестнадцатеричную строку для положительных значений. Это делает возможной реверсивную обработку BigInteger с помощью методов ToString и Parse, как показано в следующем примере.

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 {negativeNumber:N0} to {negativeHex} back to {negativeNumber2:N0}.");
Console.WriteLine($"Converted {positiveNumber:N0} to {positiveHex} back to {positiveNumber2:N0}.");
// 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.

Однако шестнадцатеричные строки, созданные путем вызова методов ToString других целочисленных типов или перегрузки метода ToString, включающего параметр toBase, не указывают знак значения или исходный тип данных, из которого была получена шестнадцатеричная строка. Для успешного создания экземпляра значения BigInteger из такой строки требуется дополнительная логика. В следующем примере представлена одна возможная реализация.

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 {positiveNumber} to {hexValue1.Value} and back to {positiveBigInt}.");

        hexString = (hexValue2.Sign == 1 ? "0" : "") + hexValue2.Value;
        negativeBigInt = BigInteger.Parse(hexString, NumberStyles.HexNumber);
        Console.WriteLine($"Converted {negativeNumber} to {hexValue2.Value} and back to {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.