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

C#BitConverter.ToString(Byte [])メソッド


C#のBitConverter.ToString()メソッドは、指定されたバイト配列の各要素の数値を同等の16進文字列表現に変換するために使用されます。

構文

public static string ToString (byte[] val);

上記では、valはバイト配列です。

using System;
public class Demo {
   public static void Main() {
      byte[] arr = {0, 10, 2, 5, 32, 45};
      int count = arr.Length;
      Console.Write("Byte Array... ");
      for (int i = 0; i < count; i++) {
         Console.Write("\n"+arr[i]);
      }
      Console.WriteLine("\nByte Array (String representation) = {0} ",
      BitConverter.ToString(arr));
   }
}

出力

Byte Array...
0
10
2
5
32
45
Byte Array (String representation) = 00-0A-02-05-20-2D

using System;
public class Demo {
   public static void Main() {
      byte[] arr = { 0, 0, 7, 10, 18, 20, 25, 26, 32};
      int count = arr.Length;
      Console.Write("Byte Array... ");
      for (int i = 0; i < count; i++) {
         Console.Write("\n"+arr[i]);
      }
      Console.WriteLine("\nByte Array (String representation) = "+BitConverter.ToString(arr));
      for (int i = 0; i < arr.Length - 1; i = i + 2) {
         short res = BitConverter.ToInt16(arr, i);
         Console.WriteLine("\nValue = "+arr[i]);
         Console.WriteLine("Result = "+res);
      }
   }
}

出力

Byte Array...
0
0
7
10
18
20
25
26
32
Byte Array (String representation) = 00-00-07-0A-12-14-19-1A-20
Value = 0
Result = 0
Value = 7
Result = 2567
Value = 18
Result = 5138
Value = 25
Result = 6681

  1. C#で配列のToString()メソッドを使用するにはどうすればよいですか?

    ToString()メソッドは、現在のオブジェクトを表す文字列を返します。 以下の例では、ToString()メソッドを別のArrayクラスメソッドで使用しています。 arr.GetLowerBound(0).ToString() 例 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace lower {    class Program {       static void Main(string[] a

  2. C#のArray.BinarySearchメソッド

    BinarySearchメソッドを使用して配列要素の場所を取得します。 文字列配列を設定する- string[] str = { "a", "m", "i", "t"}; 次に、Array.BinarySearch-を使用して、文字「t」の位置を取得します。 Array.BinarySearch(str, "t"); これが完全なコードです- 例 using System; using System.Text; public class Demo {    public