BitConverter.ToInt32 메서드

정의

오버로드

ToInt32(ReadOnlySpan<Byte>)

읽기 전용 바이트 범위를 32비트 부호 있는 정수로 변환합니다.

ToInt32(Byte[], Int32)

4바이트에서 변환된 32비트 부호 있는 정수를 바이트 배열의 지정된 위치에 반환합니다.

ToInt32(ReadOnlySpan<Byte>)

읽기 전용 바이트 범위를 32비트 부호 있는 정수로 변환합니다.

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

매개 변수

value
ReadOnlySpan<Byte>

변환할 바이트를 포함하는 읽기 전용 범위입니다.

반환

Int32

변환된 바이트를 나타내는 32비트 부호 있는 정수입니다.

예외

value의 길이가 4보다 작은 경우

적용 대상

ToInt32(Byte[], Int32)

4바이트에서 변환된 32비트 부호 있는 정수를 바이트 배열의 지정된 위치에 반환합니다.

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

매개 변수

value
Byte[]

변환할 4바이트를 포함하는 바이트 배열입니다.

startIndex
Int32

value 내의 시작 위치입니다.

반환

Int32

startIndex에서 시작하고 4바이트로 형성된 32비트 부호 있는 정수입니다.

예외

startIndexvalue의 길이에서 3을 뺀 값보다 크거나 같고 value의 길이에서 1을 뺀 값보다 작거나 같은 경우

value이(가) null인 경우

startIndex가 0보다 작거나 value - 1의 길이보다 큰 경우

예제

다음 예제에서는 메서드를 ToInt32 사용하여 4바이트 배열과 8바이트 배열의 상위 4바이트에서 값을 만듭니 Int32 다. 또한 값과 메서드를 GetBytes(Int32) 사용하여 값을 왕복합니다Int32.ToInt32

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)

설명

이 메서드는 ToInt32 바이트를 인덱 startIndex 스에서 + 3으로 startIndex 변환합니다 Int32 . 배열의 바이트 순서는 컴퓨터 시스템 아키텍처의 엔디언성을 반영해야 합니다. 자세한 내용은 의 설명 섹션 BitConverter을 참조하세요.

추가 정보

적용 대상