BitConverter.ToInt32 BitConverter.ToInt32 BitConverter.ToInt32 BitConverter.ToInt32 Method

Definizione

Overload

ToInt32(Byte[], Int32) ToInt32(Byte[], Int32) ToInt32(Byte[], Int32) ToInt32(Byte[], Int32)

Restituisce un intero con segno a 32 bit convertito da quattro byte nella posizione specificata in una matrice di byte.Returns a 32-bit signed integer converted from four bytes at a specified position in a byte array.

ToInt32(ReadOnlySpan<Byte>) ToInt32(ReadOnlySpan<Byte>) ToInt32(ReadOnlySpan<Byte>) ToInt32(ReadOnlySpan<Byte>)

ToInt32(Byte[], Int32) ToInt32(Byte[], Int32) ToInt32(Byte[], Int32) ToInt32(Byte[], Int32)

Restituisce un intero con segno a 32 bit convertito da quattro byte nella posizione specificata in una matrice di byte.Returns a 32-bit signed integer converted from four bytes at a specified position in a byte array.

public:
 static int ToInt32(cli::array <System::Byte> ^ value, int startIndex);
public static int ToInt32 (byte[] value, int startIndex);
static member ToInt32 : byte[] * int -> int
Public Shared Function ToInt32 (value As Byte(), startIndex As Integer) As Integer

Parametri

value
Byte[]

Matrice di byte.An array of bytes.

startIndex
Int32 Int32 Int32 Int32

Posizione iniziale all'interno di value.The starting position within value.

Restituisce

Un intero con segno a 32 bit formato da quattro byte a partire da startIndex.A 32-bit signed integer formed by four bytes beginning at startIndex.

Eccezioni

startIndex è maggiore o uguale alla lunghezza di value meno 3 e minore o uguale alla lunghezza di value meno 1.startIndex is greater than or equal to the length of value minus 3, and is less than or equal to the length of value minus 1.

startIndexè minore di zero o maggiore della lunghezza di value meno 1.startIndex is less than zero or greater than the length of value minus 1.

Esempi

Nell'esempio seguente viene usato ToInt32 il metodo per Int32 creare valori da una matrice di quattro byte e dai quattro byte superiori di una matrice di otto byte.The following example uses the ToInt32 method to create Int32 values from a four-byte array and from the upper four bytes of an eight-byte array. USA inoltre i metodi GetBytes(Int32) e ToInt32 per eseguire il round trip di Int32 un valore.It also uses the GetBytes(Int32) and ToInt32 methods to round-trip an Int32 value.

using System;

public class Example
{
   public static void Main()
   {
      // Create an Integer from a 4-byte array.
      Byte[] bytes1 = { 0xEC, 0x00, 0x00, 0x00 };
      Console.WriteLine("{0}--> 0x{1:X4} ({1:N0})", FormatBytes(bytes1),
                                      BitConverter.ToInt32(bytes1, 0));
      // Create an Integer from the upper four bytes of a byte array.
      Byte[] bytes2 = BitConverter.GetBytes(Int64.MaxValue / 2);
      Console.WriteLine("{0}--> 0x{1:X4} ({1:N0})", FormatBytes(bytes2),
                                      BitConverter.ToInt32(bytes2, 4));
      
      // Round-trip an integer value.
      int original = (int) Math.Pow(16, 3);
      Byte[] bytes3 = BitConverter.GetBytes(original);
      int restored = BitConverter.ToInt32(bytes3, 0);
      Console.WriteLine("0x{0:X4} ({0:N0}) --> {1} --> 0x{2:X4} ({2:N0})", original, 
                        FormatBytes(bytes3), restored);
   }

   private static string FormatBytes(Byte[] bytes)
   {
       string value = "";
       foreach (var byt in bytes)
          value += String.Format("{0:X2} ", byt);

       return value;
   }
}
// The example displays the following output:
//       EC 00 00 00 --> 0x00EC (236)
//       FF FF FF FF FF FF FF 3F --> 0x3FFFFFFF (1,073,741,823)
//       0x1000 (4,096) --> 00 10 00 00  --> 0x1000 (4,096)
Module Example
   Public Sub Main()
      ' Create an Integer from a 4-byte array.
      Dim bytes1() As Byte = { &hEC, &h00, &h00, &h00 }
      Console.WriteLine("{0}--> 0x{1:X4} ({1:N0})", FormatBytes(bytes1),
                                      BitConverter.ToInt32(bytes1, 0))
      ' Create an Integer from the upper four bytes of a byte array.
      Dim bytes2() As Byte = BitConverter.GetBytes(Int64.MaxValue \ 2)
      Console.WriteLine("{0}--> 0x{1:X4} ({1:N0})", FormatBytes(bytes2),
                                      BitConverter.ToInt32(bytes2, 4))
      
      ' Round-trip an integer value.
      Dim original As Integer = CInt(16^3)
      Dim bytes3() As Byte = BitConverter.GetBytes(original)
      Dim restored As Integer = BitConverter.ToInt32(bytes3, 0)
      Console.WriteLine("0x{0:X4} ({0:N0}) --> {1} --> 0x{2:X4} ({2:N0})", original, 
                        FormatBytes(bytes3), restored)
   End Sub
   
   Private Function FormatBytes(bytes() As Byte) As String
       Dim value As String = ""
       For Each byt In bytes
          value += String.Format("{0:X2} ", byt)
       Next
       Return value
   End Function
End Module
' The example displays the following output:
'       EC 00 00 00 --> 0x00EC (236)
'       FF FF FF FF FF FF FF 3F --> 0x3FFFFFFF (1,073,741,823)
'       0x1000 (4,096) --> 00 10 00 00  --> 0x1000 (4,096)

Commenti

Il ToInt32 metodo converte i byte dall'indice startIndex a startIndex + 3 in un Int32 valore.The ToInt32 method converts the bytes from index startIndex to startIndex + 3 to an Int32 value. L'ordine dei byte nella matrice deve riflettere la caratteristica dell'architettura del computer; Per ulteriori informazioni, vedere la sezione Osservazioni di BitConverter.The order of bytes in the array must reflect the endianness of the computer system's architecture; for more information, see the Remarks section of BitConverter.

Vedi anche

ToInt32(ReadOnlySpan<Byte>) ToInt32(ReadOnlySpan<Byte>) ToInt32(ReadOnlySpan<Byte>) ToInt32(ReadOnlySpan<Byte>)

public:
 static int ToInt32(ReadOnlySpan<System::Byte> value);
public static int ToInt32 (ReadOnlySpan<byte> value);
static member ToInt32 : ReadOnlySpan<byte> -> int
Public Shared Function ToInt32 (value As ReadOnlySpan(Of Byte)) As Integer

Parametri

Restituisce

Si applica a