BitConverter.ToInt32 Método
Importante
Parte de la información hace referencia a la versión preliminar del producto, que puede haberse modificado sustancialmente antes de lanzar la versión definitiva. Microsoft no otorga ninguna garantía, explícita o implícita, con respecto a la información proporcionada aquí.
ToInt32(ReadOnlySpan<Byte>) |
Convierte un intervalo de bytes de solo lectura en un entero de 32 bits con signo. |
ToInt32(Byte[], Int32) |
Devuelve un entero de 32 bits con signo convertido a partir de cuatro bytes en la posición especificada de una matriz de bytes. |
- Source:
- BitConverter.cs
- Source:
- BitConverter.cs
- Source:
- BitConverter.cs
Convierte un intervalo de bytes de solo lectura en un entero de 32 bits con signo.
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
Parámetros
- value
- ReadOnlySpan<Byte>
Intervalo de solo lectura que contiene los bytes que se van a convertir.
Devoluciones
Entero de 32 bits con signo que representa los bytes convertidos.
Excepciones
La longitud de value
es menor que 4.
Se aplica a
.NET 10 e outras versións
Produto | Versións |
---|---|
.NET | Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10 |
.NET Standard | 2.1 |
- Source:
- BitConverter.cs
- Source:
- BitConverter.cs
- Source:
- BitConverter.cs
Devuelve un entero de 32 bits con signo convertido a partir de cuatro bytes en la posición especificada de una matriz de bytes.
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
Parámetros
- value
- Byte[]
Matriz de bytes que incluye los cuatro bytes que se van a convertir.
- startIndex
- Int32
Posición inicial de value
.
Devoluciones
Entero de 32 bits con signo formado por cuatro bytes que comienzan en startIndex
.
Excepciones
El parámetro startIndex
es mayor o igual que la longitud de value
menos 3 y es menor o igual que la longitud de value
menos 1.
value
es null
.
startIndex
es menor que cero o mayor que la longitud de value
menos 1.
Ejemplos
En el ejemplo siguiente se usa el ToInt32 método para crear Int32 valores a partir de una matriz de cuatro bytes y de los cuatro bytes superiores de una matriz de ocho bytes. También usa los GetBytes(Int32) métodos y para realizar un recorrido de ida y ToInt32 vuelta un Int32 valor.
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)
open System
let formatBytes (bytes: byte []) =
bytes
|> Array.map (fun x -> $"{x:X2}")
|> String.concat ""
// Create an Integer from a 4-byte array.
let bytes1 = [| 0xECuy; 0x00uy; 0x00uy; 0x00uy |]
let int1 = BitConverter.ToInt32(bytes1, 0)
printfn $"{formatBytes bytes1}--> 0x{int1:X4} ({int1:N0})"
// Create an Integer from the upper four bytes of a byte array.
let bytes2 = BitConverter.GetBytes(Int64.MaxValue / 2L)
let int2 = BitConverter.ToInt32(bytes2, 4)
printfn $"{formatBytes bytes2}--> 0x{int2:X4} ({int2:N0})"
// Round-trip an integer value.
let original = pown 16 3
let bytes3 = BitConverter.GetBytes original
let restored = BitConverter.ToInt32(bytes3, 0)
printfn $"0x{original:X4} ({original:N0}) --> {formatBytes bytes3} --> 0x{restored:X4} ({restored:N0})"
// 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)
Comentarios
El ToInt32 método convierte los bytes del índice startIndex
a startIndex
+ 3 en un Int32 valor. El orden de bytes de la matriz debe reflejar la endianidad de la arquitectura del sistema informático. Para obtener más información, consulte la sección Comentarios de BitConverter.
Consulte también
Se aplica a
.NET 10 e outras versións
Produto | Versións |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
Comentarios de .NET
.NET é un proxecto de código aberto. Selecciona unha ligazón para ofrecer comentarios: