BitConverter.ToInt32 Methode
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Überlädt
| Name | Beschreibung |
|---|---|
| ToInt32(ReadOnlySpan<Byte>) |
Wandelt einen schreibgeschützten Bytebereich in eine 32-Bit-Ganzzahl um. |
| ToInt32(Byte[], Int32) |
Gibt eine 32-Bit-Ganzzahl zurück, die von vier Bytes an einer angegebenen Position in einem Bytearray konvertiert wird. |
ToInt32(ReadOnlySpan<Byte>)
- Quelle:
- BitConverter.cs
- Quelle:
- BitConverter.cs
- Quelle:
- BitConverter.cs
- Quelle:
- BitConverter.cs
- Quelle:
- BitConverter.cs
Wandelt einen schreibgeschützten Bytebereich in eine 32-Bit-Ganzzahl um.
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
Parameter
- value
- ReadOnlySpan<Byte>
Eine schreibgeschützte Spanne, die die zu konvertierenden Bytes enthält.
Gibt zurück
Eine 32-Bit-Ganzzahl, die die konvertierten Bytes darstellt.
Ausnahmen
Die Länge beträgt value kleiner als 4.
Gilt für:
ToInt32(Byte[], Int32)
- Quelle:
- BitConverter.cs
- Quelle:
- BitConverter.cs
- Quelle:
- BitConverter.cs
- Quelle:
- BitConverter.cs
- Quelle:
- BitConverter.cs
Gibt eine 32-Bit-Ganzzahl zurück, die von vier Bytes an einer angegebenen Position in einem Bytearray konvertiert wird.
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
Parameter
- value
- Byte[]
Ein Bytearray, das die vier zu konvertierenden Bytes enthält.
- startIndex
- Int32
Die Startposition innerhalb valuevon .
Gibt zurück
Eine 32-Bit-ganzzahlige Vorzeichen, die von vier Bytes gebildet wird, beginnend bei startIndex.
Ausnahmen
startIndex ist größer oder gleich der Länge von value Minus 3 und ist kleiner oder gleich der Länge von value Minus 1.
value ist null.
startIndex ist kleiner als 0 oder größer als die Länge von value minus 1.
Beispiele
Im folgenden Beispiel wird die ToInt32 Methode verwendet, um Werte aus einem 4-Byte-Array und aus den oberen vier Bytes eines Achtbyte-Arrays zu erstellen Int32 . Außerdem werden die GetBytes(Int32) Methoden und ToInt32 Methoden verwendet, um einen Int32 Wert abzurunden.
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)
Hinweise
Die ToInt32 Methode konvertiert die Bytes von Index startIndex in startIndex +3 in einen Int32 Wert. Die Reihenfolge der Bytes im Array muss die Endianität der Architektur des Computersystems widerspiegeln. Weitere Informationen finden Sie im Abschnitt "Hinweise" von BitConverter.