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("\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
热门推荐
10 朋友新年祝福语大全 简短
11 新年祝福语简短大方兔年
12 搬新家礼物祝福语简短
13 同学见面花束祝福语简短
14 五一假期祝福语幽默简短
15 离职欢送敬酒祝福语简短
16 对学弟的祝福语简短
17 考老师辞职祝福语简短
18 祝福语驱散霉运的话简短