當前位置: 首頁>>代碼示例>>C#>>正文


C# BigInteger結構體代碼示例

本文整理匯總了C#中System.Numerics.BigInteger結構體的典型用法代碼示例。如果您正苦於以下問題:C# BigInteger結構體的具體用法?C# BigInteger怎麽用?C# BigInteger使用的例子?那麽, 這裏精選的結構體代碼示例或許可以為您提供幫助。


BigInteger結構體屬於System.Numerics命名空間,在下文中一共展示了BigInteger結構體的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: BigInteger

BigInteger bigIntFromDouble = new BigInteger(179032.6541);
Console.WriteLine(bigIntFromDouble);
BigInteger bigIntFromInt64 = new BigInteger(934157136952);
Console.WriteLine(bigIntFromInt64);
開發者ID:.NET開發者,項目名稱:System.Numerics,代碼行數:4,代碼來源:BigInteger

輸出:

179032
934157136952

示例2:

long longValue = 6315489358112;      
BigInteger assignedFromLong = longValue;
Console.WriteLine(assignedFromLong);
開發者ID:.NET開發者,項目名稱:System.Numerics,代碼行數:3,代碼來源:BigInteger

輸出:

6315489358112

示例3:

BigInteger assignedFromDouble = (BigInteger) 179032.6541;
Console.WriteLine(assignedFromDouble);   
BigInteger assignedFromDecimal = (BigInteger) 64312.65m;      
Console.WriteLine(assignedFromDecimal);
開發者ID:.NET開發者,項目名稱:System.Numerics,代碼行數:4,代碼來源:BigInteger

輸出:

179032
64312

示例4: 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);
開發者ID:.NET開發者,項目名稱:System.Numerics,代碼行數:3,代碼來源:BigInteger

輸出:

The value of newBigInt is 4759477275222530853130 (or 0x102030405060708090a).

示例5: catch

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);
開發者ID:.NET開發者,項目名稱:System.Numerics,代碼行數:20,代碼來源:BigInteger

輸出:

9.1389681247993671255432112E+31
-9.0315837410896312071002088037E+34

示例6:

BigInteger number = BigInteger.Multiply(Int64.MaxValue, 3);
number++;
Console.WriteLine(number);
開發者ID:.NET開發者,項目名稱:System.Numerics,代碼行數:3,代碼來源:BigInteger

示例7: if

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++;
}
開發者ID:.NET開發者,項目名稱:System.Numerics,代碼行數:10,代碼來源:BigInteger

示例8: if

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;
開發者ID:.NET開發者,項目名稱:System.Numerics,代碼行數:12,代碼來源:BigInteger

示例9: foreach

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);
開發者ID:.NET開發者,項目名稱:System.Numerics,代碼行數:14,代碼來源:BigInteger

輸出:

8.5070591730234615847396907784E+37
0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0x3F

8.5070591730234615847396907784E+37

示例10: foreach

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);
開發者ID:.NET開發者,項目名稱:System.Numerics,代碼行數:14,代碼來源:BigInteger

輸出:

30000
0x30 0x75
30000

示例11: BigInteger

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"));
開發者ID:.NET開發者,項目名稱:System.Numerics,代碼行數:12,代碼來源:BigInteger

輸出:

-1,000,000
4,293,967,296

示例12: foreach

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"));
開發者ID:.NET開發者,項目名稱:System.Numerics,代碼行數:21,代碼來源:BigInteger

輸出:

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

示例13:

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);
開發者ID:.NET開發者,項目名稱:System.Numerics,代碼行數:16,代碼來源:BigInteger

輸出:

Converted -1,000,000 to F0BDC0 back to -1,000,000.
Converted 15,777,216 to 0F0BDC0 back to 15,777,216.

示例14: Main

//引入命名空間
using System;
using System.Globalization;
using System.Numerics;

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

public class Example
{
   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);
   }
}
開發者ID:.NET開發者,項目名稱:System.Numerics,代碼行數:41,代碼來源:BigInteger

輸出:

Converted 4039543321 to F0C68A19 and back to 4039543321.
Converted -255423975 to f0c68a19 and back to -255423975.


注:本文中的System.Numerics.BigInteger結構體示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。