zhao
2021-07-19 8347f2fbddbd25369359dcb2da1233ac48a19fdc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using System.Text;
using System.Collections.Generic;
 
namespace QiHe.CodeLib
{
    /// <summary>
    /// Binary data to Hexadecimal string
    /// </summary>
    public class Bin2Hex
    {
        /// <summary>
        /// Encodes the specified data.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <returns></returns>
        public static string Encode(byte[] data)
        {
            StringBuilder code = new StringBuilder(data.Length * 2);
            foreach (byte bt in data)
            {
                code.Append(bt.ToString("X2"));
            }
            return code.ToString();
        }
 
        /// <summary>
        /// Decodes the specified code.
        /// </summary>
        /// <param name="code">The code.</param>
        /// <returns></returns>
        public static byte[] Decode(string code)
        {
            byte[] bytes = new byte[code.Length / 2];
            for (int i = 0; i < bytes.Length; i++)
            {
                bytes[i] = byte.Parse(code.Substring(i * 2, 2), System.Globalization.NumberStyles.HexNumber);
            }
            return bytes;
        }
 
        /// <summary>
        /// Formats the specified data.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <returns></returns>
        public static string Format(byte[] data)
        {
            return Format(data, "{0:X2} ", 16);
        }
 
        /// <summary>
        /// Formats the specified data.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <param name="format">The format.</param>
        /// <param name="bytesPerRow">The bytes per row.</param>
        /// <returns></returns>
        public static string Format(byte[] data, string format, int bytesPerRow)
        {
            StringBuilder code = new StringBuilder();
            int count = 0;
            foreach (byte bt in data)
            {
                code.AppendFormat(format, bt);
                count++;
                if (count == bytesPerRow)
                {
                    code.AppendLine();
                    count = 0;
                }
            }
            return code.ToString();
        }
 
        /// <summary>
        /// Parses the specified text.
        /// </summary>
        /// <param name="text">The text.</param>
        /// <returns></returns>
        public static byte[] Parse(string text)
        {
            List<byte> bytes = new List<byte>();
            char[] chars = new char[2];
            bool start = true;
            foreach (char ch in text)
            {
                if (char.IsLetterOrDigit(ch))
                {
                    if (start)
                    {
                        chars[0] = ch;
                        start = false;
                    }
                    else
                    {
                        chars[1] = ch;
                        bytes.Add(Convert.ToByte(new string(chars), 16));
                        start = true;
                    }
                }
            }
            return bytes.ToArray();
        }
    }
}