BigInteger.ToByteArray Metodo

Definizione

Overload

ToByteArray()

Converte un valore BigInteger in una matrice di byte.

ToByteArray(Boolean, Boolean)

Restituisce il valore di BigInteger come matrice di byte usando il minor numero possibile di byte. Se il valore è zero, restituisce una matrice di un byte il cui elemento è 0x00.

ToByteArray()

Source:
BigInteger.cs
Source:
BigInteger.cs
Source:
BigInteger.cs

Converte un valore BigInteger in una matrice di byte.

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

Restituisce

Byte[]

Valore dell'oggetto BigInteger corrente convertito in una matrice di byte.

Esempio

Nell'esempio seguente viene illustrato il modo in cui alcuni BigInteger valori sono rappresentati in matrici di byte.

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

Commenti

I singoli byte nella matrice restituita da questo metodo vengono visualizzati in ordine little-endian. Ovvero, i byte di ordine inferiore del valore precedono i byte di ordine superiore. Il primo byte della matrice riflette i primi otto bit del BigInteger valore, il secondo byte riflette gli otto bit successivi e così via. Ad esempio, il valore 1024 o 0x0400, viene archiviato come matrice di due byte seguente:

Elemento Valore byte
0 0x00
1 0x04

I valori negativi vengono scritti nella matrice usando la rappresentazione di complemento di due nel formato più compatto possibile. Ad esempio, -1 è rappresentato come un singolo byte il cui valore è 0xFF anziché come matrice con più elementi, ad 0xFFesempio , 0xFF o 0xFF, 0xFF, , 0xFF0xFF.

Poiché la rappresentazione di complemento di due interpreta sempre il bit di ordine più alto dell'ultimo byte nella matrice (il byte nella posizione Array.Length- 1) come bit di segno, il metodo restituisce una matrice di byte con un elemento aggiuntivo il cui valore è zero per disambiguare i valori positivi che altrimenti potrebbero essere interpretati come con i relativi bit di segno impostati. Ad esempio, il valore 120 o 0x78 è rappresentato come matrice a byte singolo: 0x78. Tuttavia, 128 o 0x80, è rappresentato come matrice a due byte: 0x80, 0x00.

È possibile eseguire il round trip di un BigInteger valore archiviandolo in una matrice di byte e quindi ripristinandolo usando il BigInteger(Byte[]) costruttore .

Attenzione

Se il codice modifica il valore di singoli byte nella matrice restituita da questo metodo prima di ripristinare il valore, è necessario assicurarsi di non modificare involontariamente il bit del segno. Ad esempio, se le modifiche aumentano un valore positivo in modo che il bit più alto nell'ultimo elemento della matrice di byte diventi impostato, è possibile aggiungere un nuovo byte il cui valore è zero alla fine della matrice.

Si applica a

ToByteArray(Boolean, Boolean)

Source:
BigInteger.cs
Source:
BigInteger.cs
Source:
BigInteger.cs

Restituisce il valore di BigInteger come matrice di byte usando il minor numero possibile di byte. Se il valore è zero, restituisce una matrice di un byte il cui elemento è 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()

Parametri

isUnsigned
Boolean

true per usare la codifica senza segno; in caso contrario, false.

isBigEndian
Boolean

true per scrivere i byte in un ordine di byte big endian; in caso contrario, false.

Restituisce

Byte[]

Valore dell'oggetto BigInteger corrente convertito in una matrice di byte.

Eccezioni

Se isUnsigned è true e Sign è negativo.

Commenti

Il valore 33022 intero può essere esportato in quattro matrici diverse:

Proprietà Risultato
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 }

Si applica a