BigInteger.ToByteArray 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
多載
| 名稱 | Description |
|---|---|
| ToByteArray() |
將一個 BigInteger 值轉換成位元組陣列。 |
| ToByteArray(Boolean, Boolean) |
會以盡可能少的位元組數回傳這個值 BigInteger ,以位元組陣列的形式出現。 若值為零,則回傳一個一位元組的陣列,元素為 0x00。 |
ToByteArray()
將一個 BigInteger 值轉換成位元組陣列。
public:
cli::array <System::Byte> ^ ToByteArray();
public byte[] ToByteArray();
member this.ToByteArray : unit -> byte[]
Public Function ToByteArray () As 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
open System
open System.Numerics
let numbers =
[| BigInteger.MinusOne
BigInteger.One
BigInteger.Zero
120
128
255
1024
Int64.MinValue
Int64.MaxValue
BigInteger.Parse("90123123981293054321") |]
for number in numbers do
let bytes = number.ToByteArray()
printf $"""{number} ({number.ToString("X" + (bytes.Length * 2).ToString())}) -> """
printf $"{bytes.Length} bytes: "
for byteValue in bytes do
printf $"{byteValue:X2} "
printfn ""
// 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為 ,而非包含多個元素的陣列,例如 0xFF、 0xFF0xFF0xFF0xFF0xFF。
由於 2 的補數表示法總是將陣列中最後一個位元組的最高階位元(位置 Array.Length- 1位元組)解讀為符號位元,該方法會回傳一個額外元素的位元組陣列,該元素的值為零,以消除可能被解讀為符號位元設定的正值。 例如,值 120 或 0x78 表示為一個單位元組陣列: 0x78。 然而,128 或 0x80則以兩個位元組陣列表示: 0x80, 0x00。
你可以透過將一個值儲存到位元組陣列,然後用BigInteger建構子還原來進行循環BigInteger(Byte[])。
注意事項
如果你的程式碼在還原該值之前修改了陣列中各個位元組的值,你必須確保不會無意中更改符號位元。 例如,如果你的修改增加一個正值,使得位元組陣列最後一個元素的最高階位元被設定為固定,你可以在陣列末端新增一個值為零的新位元組。
適用於
ToByteArray(Boolean, Boolean)
會以盡可能少的位元組數回傳這個值 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
傳回
目前物件的值 BigInteger 會轉換成一個位元組陣列。
例外狀況
如果 isUnsigned 且 trueSign 為負。
備註
整數值 33022 可匯出成四種不同的陣列:
| 屬性 | Result |
|---|---|
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 } |