C# BitConverter.ToString(Byte[]) 方法


C# 中的 BitConverter.ToString() 方法用於將指定位元組陣列中每個元素的數字值轉換為其等效的十六進位制字串表示形式。

語法

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("
"+arr[i]);       }       Console.WriteLine("
Byte 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("
"+arr[i]);       }       Console.WriteLine("
Byte 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("
Value = "+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

更新於: 2019-12-02

1 千+ 次觀看

啟動您的 職業

完成課程並獲得認證

開始
廣告
© . All rights reserved.