BigInteger.ToByteArray 方法

定義

多載

ToByteArray()

BigInteger 值轉換成位元組陣列。

ToByteArray(Boolean, Boolean)

使用盡可能最少的位元組數,傳回這個 BigInteger 的值作為位元組陣列。 如果值為零,則傳回其元素為 0x00 的一個位元組陣列。

ToByteArray()

來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs

BigInteger 值轉換成位元組陣列。

public:
 cli::array <System::Byte> ^ ToByteArray();
public byte[] ToByteArray ();
member this.ToByteArray : unit -> byte[]
Public Function ToByteArray () As Byte()

傳回

Byte[]

目前 BigInteger 物件的值,已轉換為位元組陣列。

範例

下列範例說明如何在位元組陣列中表示某些 BigInteger 值。

using System;
using System.Numerics;

public class Example
{
   static byte[] bytes;

   public static void Main()
   {
      BigInteger[] numbers = { BigInteger.MinusOne, BigInteger.One,
                               BigInteger.Zero, 120, 128, 255, 1024,
                               Int64.MinValue, Int64.MaxValue,
                               BigInteger.Parse("90123123981293054321") };
      foreach (BigInteger number in numbers)
      {
         bytes = number.ToByteArray();
         Console.Write("{0} ({1}) -> ", number, number.ToString(GetSpecifier()));
         Console.Write("{0} bytes: ", bytes.Length);
         foreach (byte byteValue in bytes)
            Console.Write("{0:X2} ", byteValue);

         Console.WriteLine();
      }
   }

   private static string GetSpecifier()
   {
      return "X" + (bytes.Length * 2).ToString();
   }
}
// The example displays the following output:
//    -1 (FF) -> 1 bytes: FF
//    1 (01) -> 1 bytes: 01
//    0 (00) -> 1 bytes: 00
//    120 (78) -> 1 bytes: 78
//    128 (0080) -> 2 bytes: 80 00
//    255 (00FF) -> 2 bytes: FF 00
//    1024 (0400) -> 2 bytes: 00 04
//    -9223372036854775808 (8000000000000000) -> 8 bytes: 00 00 00 00 00 00 00 80
//    9223372036854775807 (7FFFFFFFFFFFFFFF) -> 8 bytes: FF FF FF FF FF FF FF 7F
//    90123123981293054321 (04E2B5A7C4A975E971) -> 9 bytes: 71 E9 75 A9 C4 A7 B5 E2 04
Imports System.Numerics

Module Example
   Dim bytes() As Byte
      
   Public Sub Main()
      Dim numbers() As BigInteger = { BigInteger.MinusOne, BigInteger.One, 
                                      BigInteger.Zero, 120, 128, 255, 1024, 
                                      Int64.MinValue, Int64.MaxValue, 
                                      BigInteger.Parse("90123123981293054321") }
      For Each number As BigInteger In numbers
         bytes = number.ToByteArray()
         Console.Write("{0} ({1}) -> ", number, number.ToString(GetSpecifier()))
         Console.Write("{0} bytes: ", bytes.Length)
         For Each byteValue As Byte In bytes
            Console.Write("{0:X2} ", byteValue)
         Next
         Console.WriteLine()
      Next   
   End Sub
   
   Private Function GetSpecifier() As String
      Return "X" + CStr(bytes.Length * 2)
   End Function
End Module
' The example displays the following output:
'    -1 (FF) -> 1 bytes: FF
'    1 (01) -> 1 bytes: 01
'    0 (00) -> 1 bytes: 00
'    120 (78) -> 1 bytes: 78
'    128 (0080) -> 2 bytes: 80 00
'    255 (00FF) -> 2 bytes: FF 00
'    1024 (0400) -> 2 bytes: 00 04
'    -9223372036854775808 (8000000000000000) -> 8 bytes: 00 00 00 00 00 00 00 80
'    9223372036854775807 (7FFFFFFFFFFFFFFF) -> 8 bytes: FF FF FF FF FF FF FF 7F
'    90123123981293054321 (04E2B5A7C4A975E971) -> 9 bytes: 71 E9 75 A9 C4 A7 B5 E2 04

備註

這個方法所傳回之陣列中的個別位元組會以小到尾的順序顯示。 也就是說,該值的較低順序位元組位於較高順序位元組之前。 陣列的第一個位元組會反映值的前八個位 BigInteger 、第二個位元組反映下一個八位等等。 例如,值 1024 或0x0400會儲存為下列兩個位元組的陣列:

項目 位元組值
0 0x00
1 0x04

負值會以最精簡的形式,使用兩個補數標記法寫入陣列。 例如,-1 是以單一位元組表示,其值為 0xFF ,而不是具有多個元素的陣列,例如 0xFF0xFF0xFF0xFF 、 、 。 0xFF0xFF

因為兩個補數標記法一律會解譯陣列中最後一個位元組的最高順序位, (位 Array.Length- 1 位置的位元組) 做為符號位,所以方法會傳回位元組陣列,其值為零的位元組陣列,以厘清可能設定其符號位的正值。 例如,值 120 或 0x78 以單一位元組陣列表示: 0x78 。 不過,128 或 0x80 會以雙位元組陣列表示: 0x800x00

您可以將 BigInteger 值儲存至位元組陣列,然後使用建構函式來回還原值 BigInteger(Byte[])

警告

如果您的程式碼在還原值之前修改此方法所傳回之陣列中的個別位元組值,您必須確定您不小心變更符號位。 例如,如果您的修改增加正值,使位元組陣列最後一個元素中的最高順序位變成設定,您可以將值為零的新位元組加入至陣列結尾。

適用於

ToByteArray(Boolean, Boolean)

來源:
BigInteger.cs
來源:
BigInteger.cs
來源:
BigInteger.cs

使用盡可能最少的位元組數,傳回這個 BigInteger 的值作為位元組陣列。 如果值為零,則傳回其元素為 0x00 的一個位元組陣列。

public byte[] ToByteArray (bool isUnsigned = false, bool isBigEndian = false);
member this.ToByteArray : bool * bool -> byte[]
Public Function ToByteArray (Optional isUnsigned As Boolean = false, Optional isBigEndian As Boolean = false) As Byte()

參數

isUnsigned
Boolean

true 表示使用不帶正負號的編碼;否則為 false

isBigEndian
Boolean

true 表示以位元組由大到小的位元組順序寫入位元組;否則為 false

傳回

Byte[]

目前 BigInteger 物件的值,已轉換為位元組陣列。

例外狀況

如果 isUnsignedtrueSign 為負值。

備註

整數值 33022 可以在四個不同的陣列中匯出:

屬性 結果
isUnsigned: false, isBigEndian: false new byte[] { 0xFE, 0x80, 0x00 }
isUnsigned: false, isBigEndian: true new byte[] { 0x00, 0x80, 0xFE }
isUnsigned: true, isBigEndian: false new byte[] { 0xFE, 0x80 }
isUnsigned: true, isBigEndian: true new byte[] { 0x80, 0xFE }

適用於