C#
 Computer >> コンピューター >  >> プログラミング >> C#

C#のBitConverter.ToInt16()メソッド


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

  1. C#のSequenceEqualメソッド

    SequenceEqualメソッドは、コレクションが等しいかどうかをテストするために使用されます。 3つの文字列配列を設定しましょう- string[] arr1 = { "This", "is", "it" }; string[] arr2 = { "My", "work", "report" }; string[] arr3 = { "This", "is", "it" }; 次に、SequenceEqual

  2. C#の集計メソッド

    C#でAggregateメソッドを使用して、Sum、Min、Max、Averageなどの数学演算を実行します。 Aggregateメソッドを使用して配列要素を乗算する例を見てみましょう。 これが私たちの配列です- int[] arr = { 10, 15, 20 }; ここで、Aggregate()メソッドを使用します- arr.Aggregate((x, y) => x * y); これが完全なコードです- 例 using System; using System.Linq; using System.IO; public class Demo {    p