C#의 BitConverter.ToInt32() 메서드는 바이트 배열의 지정된 위치에 있는 4바이트에서 변환된 32비트 부호 있는 정수를 반환하는 데 사용됩니다.
구문
구문은 다음과 같습니다 -
public static int ToInt32 (byte[] val, int begnIndex);
위에서 val은 바이트 배열이고, begnIndex는 val 내의 시작 위치입니다.
예시
이제 예를 살펴보겠습니다 -
using System;
public class Demo {
public static void Main(){
byte[] arr = { 10, 20, 30, 40, 50};
Console.WriteLine("Byte Array = {0} ", BitConverter.ToString(arr));
for (int i = 1; i < arr.Length - 3; i = i + 4) {
int res = BitConverter.ToInt32(arr, i);
Console.WriteLine("\nValue = "+arr[i]);
Console.WriteLine("Result = "+res);
}
}
} 출력
이것은 다음과 같은 출력을 생성합니다 -
Byte Array = 0A-14-1E-28-32 Value = 20 Result = 841489940
예시
이제 다른 예를 살펴보겠습니다 -
using System;
public class Demo {
public static void Main(){
byte[] arr = { 0, 0, 10, 15, 20, 26, 32, 34, 40};
Console.WriteLine("Byte Array = {0} ", BitConverter.ToString(arr));
for (int i = 1; i < arr.Length - 3; i = i + 4) {
int res = BitConverter.ToInt32(arr, i);
Console.WriteLine("\nValue = "+arr[i]);
Console.WriteLine("Result = "+res);
}
}
} 출력
이것은 다음과 같은 출력을 생성합니다 -
Byte Array = 00-00-0A-0F-14-1A-20-22-28 Value = 0 Result = 336529920 Value = 26 Result = 673325082