BigInteger.ToByteArray メソッド

定義

オーバーロード

ToByteArray()

BigInteger 値をバイト配列に変換します。

ToByteArray(Boolean, Boolean)

使用できる最も少ないバイト数を使用して、この BigInteger の値をバイト配列として返します。 値が 0 の場合は、その要素が 0x00 である 1 バイトの配列を返します。

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

注釈

このメソッドによって返される配列内の個々のバイトは、リトル エンディアン順で表示されます。 つまり、値の下位バイトが上位バイトより前になります。 配列の最初のバイトは値の最初の 8 ビットを BigInteger 反映し、2 番目のバイトは次の 8 ビットを反映します。 たとえば、値 1024 (0x0400) は、次の 2 バイトの配列として格納されます。

要素 バイト値
0 0x00
1 0x04

負の値は、可能な限り最もコンパクトな形式で 2 つの補数表現を使用して配列に書き込まれます。 たとえば、-1 は 1 バイトとして表され、値は 0xFF 、 などの 0xFF0xFF0xFF0xFF0xFF0xFF複数の要素を持つ配列として表されます。

2 の補数表現では、配列内の最後のバイトの最上位ビット (位置 Array.Length- 1のバイト) が符号ビットとして常に解釈されるため、このメソッドは、値が 0 の余分な要素を持つバイト配列を返し、それ以外の場合は符号ビットが設定されると解釈できる正の値を明確にします。 たとえば、値 120 または 0x78 は 1 バイト配列として表されます。 0x78 ただし、128 または 0x80は、2 バイト配列 0x80(、 0x00) として表されます。

値をバイト配列に BigInteger 格納し、コンストラクターを使用して復元することで、値を BigInteger(Byte[]) ラウンドトリップできます。

注意事項

コードが値を復元する前に、このメソッドによって返される配列内の個々のバイトの値を変更する場合は、意図せずに符号ビットを変更しないようにする必要があります。 たとえば、バイト配列の最後の要素の最上位ビットが設定されるように変更によって正の値が増加する場合、値が 0 の新しいバイトを配列の末尾に追加できます。

適用対象

ToByteArray(Boolean, Boolean)

ソース:
BigInteger.cs
ソース:
BigInteger.cs
ソース:
BigInteger.cs

使用できる最も少ないバイト数を使用して、この BigInteger の値をバイト配列として返します。 値が 0 の場合は、その要素が 0x00 である 1 バイトの配列を返します。

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 オブジェクトをバイトの配列に変換した値。

例外

isUnsignedtrue で、Sign が負の値の場合。

注釈

整数値 33022 は、次の 4 つの異なる配列でエクスポートできます。

Properties 結果
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 }

適用対象