C#의 BitConverter.ToInt16() 메서드는 바이트 배열의 지정된 위치에 있는 2바이트에서 변환된 16비트 부호 있는 정수를 반환하는 데 사용됩니다.
구문
구문은 다음과 같습니다 -
public static short ToInt16 (byte[] val, int begnIndex);
위에서 val은 바이트 배열이고, begnIndex는 val 내의 시작 위치입니다.
예시
이제 예를 살펴보겠습니다 -
using System;
public class Demo {
public static void Main(){
byte[] arr = { 0, 0, 7, 10, 18, 20, 25, 26, 32};
Console.WriteLine("Byte Array = {0} ", BitConverter.ToString(arr));
for (int i = 1; i < arr.Length - 1; i = i + 2) {
short res = BitConverter.ToInt16(arr, i);
Console.WriteLine("\nValue = "+arr[i]);
Console.WriteLine("Result = "+res);
}
}
} 출력
이것은 다음과 같은 출력을 생성합니다 -
Byte Array = 00-00-07-0A-12-14-19-1A-20 Value = 0 Result = 1792 Value = 10 Result = 4618 Value = 20 Result = 6420 Value = 26 Result = 8218
예시
이제 다른 예를 살펴보겠습니다 -
using System;
public class Demo {
public static void Main(){
byte[] arr = { 10, 20, 30};
Console.WriteLine("Byte Array = {0} ", BitConverter.ToString(arr));
for (int i = 1; i < arr.Length - 1; i = i + 2) {
short values = BitConverter.ToInt16(arr, i);
Console.WriteLine("\nValue = "+arr[i]);
Console.WriteLine("Result = "+values);
}
}
} 출력
이것은 다음과 같은 출력을 생성합니다 -
Byte Array = 0A-14-1E Value = 20 Result = 7700