字节数组(byte[])与16进制字符串转换

 1  /// <summary>
 2     /// 转换扩展类
 3     /// </summary>
 4     public static class ConvertExtend
 5     {
 6         /// <summary>
 7         /// 将byte[]转换为16进制字符串
 8         /// </summary>
 9         /// <param name="bytes"></param>
10         /// <returns></returns>
11         public static string ToString16(this byte[] bytes)
12         {
13             return bytes.ToList().ToString16();
14         }
15 
16         /// <summary>
17         /// 将List<byte>转换为16进制字符串
18         /// </summary>
19         /// <param name="bytes"></param>
20         /// <returns></returns>
21         public static string ToString16(this List<byte> bytes)
22         {
23             return string.Join(" ", bytes.ConvertAll(b => Convert.ToString(b, 16)));
24         }
25 
26         /// <summary>
27         /// 将16进制字符串转换为byte[]
28         /// </summary>
29         /// <param name="str16"></param>
30         /// <returns></returns>
31         public static byte[] ToByte(this string str16)
32         {
33             return str16.Split(' ').ToList().ConvertAll(s => Convert.ToByte(s, 16)).ToArray();
34         }
35     }
ConvertExtend

 

posted @ 2019-08-02 11:27  鸑鷟虽孤  阅读(2881)  评论(0编辑  收藏  举报