使用 C# 实现 Bin 类,快速方便地管理 byte[] 类型一直是我的心愿。因为在 C C++ C# VB 等语言中,byte[] 类型是常用的,但却没有太多方便的命令去处理它。因此我编写了 Bin 类。这个类实现了方便的管理,封装了大部分 Byte[] 操作,如添加、删除、查找等。在编写中注重速度、效率和方便性,支持索引器[index]直接引用,操作符重载等。

同时这也是我写的“ToEasy”C#简单化代码的一部分,namespace 就是 ToEasy 了!

下面就是我写的代码:

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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
 
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
/* Bin 操作类
 * 版本:1.0
 * --
 * 欢迎访问:http://ceeji.net (博客)
 *
 */
namespace ToEasy
{
    /// <summary>
    /// 相当于易语言中的“字节集”数据类型,可以容纳字节数组数据。简化了对数据的操作。
    /// </summary>
    public class Bin
    {
        //字段
        byte[] data = new byte[0];
 
        //属性
        public Byte[] ByteArray
        {
            get
            {
                return this.data;
            }
        }
        public int Length
        {
            get
            {
                return this.data.Length;
            }
        }
 
        //隐式转换
        public static implicit operator Bin(byte[] from)
        {
            return new Bin(from);
        }
        public static implicit operator byte[] (Bin from)
        {
            return (byte[])from.data.Clone();
        }
        //运算
        public static bool operator ==(Bin bin, byte[] o)
        {
            return bin.ByteArray == o;
        }
        public static bool operator !=(Bin bin, byte[] o)
        {
            return bin.ByteArray != o;
        }
        public static bool operator ==(byte[] o, Bin bin)
        {
            return bin == o;
        }
        public static bool operator !=(byte[] o, Bin bin)
        {
            return bin != o;
        }
        public static Bin operator +(Bin bin1, Bin bin2)
        {
            return bin1.GetAppend((byte[])bin2);
        }
        //索引器
        public byte this[int index]
        {
            get
            {
                return this.GetByte(index);
            }
            set
            {
                this.data[index] = value;
            }
        }
        //构造
        public Bin()
        {
        }
        public Bin(byte[] from)
        {
            this.FromByteArray(from);
        }
        public Bin(int length)
        {
            this.data = new byte[length];
        }
        //方法
        public void Clear()
        {
            data = new byte[0];
        }
        public void FromByteArray(byte[] byteArray)
        {
            data = (byte[])byteArray.Clone();
        }
        public void FromFile(string fileName)
        {
            this.FromByteArray(File.ReadAllBytes(fileName));
        }
        /// <summary>
        /// 向现在的字节集的末尾添加数据。
        /// </summary>
        /// <param name="append">字节集数据,可以为 byte[] 或 Bin.</param>
        public void Append(byte[] append)
        {
            byte[] nbyte = new byte[this.Length + append.Length];
            this.data.CopyTo(nbyte, 0);
            append.CopyTo(nbyte, this.Length);
            this.FromByteArray(nbyte);
        }
        public Bin GetAppend(byte[] append)
        {
            Bin bin = new Bin();
            byte[] nbyte = new byte[this.Length + append.Length];
            this.data.CopyTo(nbyte, 0);
            append.CopyTo(nbyte, this.Length);
            bin.FromByteArray(nbyte);
            return bin;
        }
        public byte GetByte(int index)
        {
            return data[index];
        }
        public byte[] GetLeft(int length)
        {
            byte[] ret = new byte[length];
            Array.Copy(data, 0, ret, 0, length);
            return ret;
        }
        public byte[] GetData(int startIndex, int length)
        {
            byte[] ret = new byte[length];
            Array.Copy(data, startIndex, ret, 0, length);
            return ret;
        }
        public byte[] GetRight(int length)
        {
            byte[] ret = new byte[length];
            Array.Copy(data, this.Length - length - 1, ret, 0, length);
            return ret;
        }
        /// <summary>
        /// 返回当前对象的字符串形式,例如 { 57, 46 }
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            if (data.Length == 0)
            {
                return "{ }";
            }
            StringBuilder str = new StringBuilder("{ ");
            foreach (byte each in data)
            {
                str.Append(each.ToString());
                str.Append(", ");
            }
            str.Remove(str.Length - 2, 2);
            str.Append(" }");
            return str.ToString();
        }
        /// <summary>
        /// 移除某项。这会很浪费时间,尽量不要这样做。
        /// </summary>
        /// <param name="startindex"></param>
        /// <param name="length"></param>
        public void Remove(int startindex, int length)
        {
            if (length < 1)
            {
                return;
            }
            byte[] ndata = new byte[this.Length - length];
            int j = 0;
            for (int i = 0; i <= this.Length - 1; i++)
            {
                if ((i >= startindex & i < = startindex + length) == false)
                {
                    ndata[j] = this[i];
                    j++;
                }
            }
            this.data = (byte[])ndata.Clone();
        }
        /// <summary>
        /// 在当前字节集中查找子字节集。如果找到则返回索引位置,否则返回 -1。
        /// 
        /// <param name="findbin">要找的数据</param>
        /// <returns>索引位置</returns>
        public int Find(Bin findbin)
        {
            byte[] b = findbin.ByteArray ;
            int l = findbin.Length;
            int z = this.Length - l + 1;
            for (int i = 0; i < = z; i++)
            {
                if (findbin.GetData(i, l) == b)
                {
                    return i;
                }
            }
            return -1;
        }
        /// <summary>
        /// 在当前字节集中从后向前查找子字节集。如果找到则返回索引位置,否则返回 -1。
        /// 
        /// <param name="findbin">要找的数据</param>
        /// <returns>索引位置</returns>
        public int FindLast(Bin findbin)
        {
            byte[] b = findbin.ByteArray;
            int l = findbin.Length;
            int z = this.Length - l + 1;
            for (int i = z; i == 0; i--)
            {
                if (findbin.GetData(i, l) == b)
                {
                    return i;
                }
            }
            return -1;
        }
        /// <summary>
        /// 执行替换。
        /// </summary>
        /// <param name="startindex">开始位置索引</param>
        /// <param name="length">替换长度</param>
        /// <param name="bin">替换成的数据</param>
        public bool Replace(int startindex, int length, Bin bin)
        {
            if (startindex > this.Length - 1)//开始位置超标
            {
                return false;
            }
            if (startindex + length > this.Length)//结束位置超标
            {
                return false;
            }
            if (bin.Length < length)//提供数据不足
            {
                return false;
            }
            byte[] b = bin.ByteArray;
            int j = 0;
            for (int i = startindex; i <= startindex + length - 1; i++)
            {
                this.data[i] = b[j];
                j++;
            }
            return true;
        }
        /// <summary>
        /// 将当前字节集写到指定的文件中。如果文件已存在,该文件将被覆盖。
        /// 
        /// <param name="filename">文件名</param>
        /// <param name="append">是否追加</param>
        public void WriteToFile(string filename, bool append)
        {
            if (append == false)
            {
                File.WriteAllBytes(filename, this.ByteArray);
            }
            else
            {
                FileStream stream = new FileStream (filename,FileMode.Append );
                BinaryWriter writer = new BinaryWriter(stream);
                writer.Write(this.ByteArray);
                writer.Close();
                stream.Close();
            }
        }
        /// <summary>
        /// 将当前字节集写入文件基础流。
        /// </summary>
        /// <param name="stream">文件基础流</param>
        public void WriteToFileStream(FileStream stream)
        {
            BinaryWriter writer = new BinaryWriter(stream);
            writer.Write(this.ByteArray);
            writer.Close();
        }
    }
}