|
我们知道易语言字节集就是处理byte[]。
虽然处理byte[] C#有丰富的方法。
但实现易语言字节集处理这个类还是有丰富的意义:
。仿写易语言功能时用
。http相关操作时用,比如图片上传等要处理byte[]时
。socket处理协yi时也是处理字节集,即byte[]
注意函数的命名跟易语言对应的指令是一致的。
- /// <summary>
- /// byte[]处理类,即e语言:字节集处理
- /// </summary>
- public static class binHelper
- {
- /// <summary>
- /// E语言功能:取重复字节集
- /// </summary>
- /// <param name="repNums"></param>
- /// <param name="repByteAry"></param>
- /// <returns></returns>
- public static byte[] bin(int repNums,byte[] repByteAry)
- {
- var tmp = new List<byte>();
- for(int i=0;i<repNums;i++)
- {
- tmp.AddRange(repByteAry);
- }
- return tmp.ToArray();
- }
- /// <summary>
- /// E语言功能:取空白字节集
- /// </summary>
- /// <param name="len">零字节数目</param>
- /// <returns></returns>
- public static byte[] SpaceBin(int len)
- {
- var tmp = new List<byte>();
- for (int i = 0; i < len; i++)
- tmp.Add(0);
- return tmp.ToArray();
- }
- /// <summary>
- /// E语言功能:字节集替换
- /// </summary>
- /// <param name="data"></param>
- /// <param name="searchString">搜索的字符串</param>
- /// <param name="reString">要替换的字符串</param>
- /// <returns></returns>
- public static byte[] RpBin(this byte[] data,string searchString,string reString)
- {
- return data.Replace1(Encoding.Default.GetBytes(searchString),
- Encoding.Default.GetBytes(reString));
- }
- /// <summary>
- /// E语言功能:字节集替换
- /// </summary>
- /// <param name="data">数据</param>
- /// <param name="searchByteAry">搜索字节集</param>
- /// <param name="repByteAry">要替换的字节集</param>
- /// <returns></returns>
- public static byte[] RpBin(this byte[] data,byte[] searchByteAry,byte[] repByteAry)
- {
- return data.Replace1(searchByteAry, repByteAry);
- }
- /// <summary>
- /// E语言功能:寻找字节集
- /// </summary>
- /// <param name="data"></param>
- /// <param name="targetObj"></param>
- /// <returns></returns>
- public static int InBin(this byte[] data,byte[] searcjObj)
- {
- int curIndex = 0, start = -1,end = -1,searIndex = 0;
- for (int x = 0; x < data.Length; x++)
- {
- curIndex++;
- if (data[x] == searcjObj[searIndex])
- {
- if (searIndex == 0)
- {
- start = x;
- }
- if (searIndex == searcjObj.Length - 1)
- {
- end = x;
- searIndex = 0;
- }
- else
- {
- searIndex++;
- }
- }
- else
- {
- searIndex = 0;
- }
- }
- return start;
- }
- /// <summary>
- /// E语言功能:取字节集中间
- /// </summary>
- /// <param name="data"></param>
- /// <param name="startPos"></param>
- /// <param name="len"></param>
- /// <returns></returns>
- public static byte[] BinMid(this byte[] data,int startPos,int len)
- {
- try
- {
- return data.Range(startPos, len).ToArray();
- }
- catch(Exception ex)
- {
- throw new ArgumentException(ex.Message);
- }
- }
- /// <summary>
- /// E语言功能:取字节集右边
- /// </summary>
- /// <param name="data"></param>
- /// <param name="len"></param>
- /// <returns></returns>
- public static byte[] BinRight(this byte[] data, int len)
- {
- try
- {
- var res = data.Tail(len);
- return res.ToArray();
- }
- catch(Exception ex)
- {
- throw new ArgumentException(ex.Message);
- }
- }
- /// <summary>
- /// E语言功能:取字节集左边
- /// </summary>
- /// <param name="data"></param>
- /// <param name="len"></param>
- /// <returns></returns>
- public static byte[] BinLeft(this byte[] data,int len)
- {
- try
- {
- var res = data.Take(len).ToArray();
- return res;
- }
- catch(Exception ex)
- {
- throw new ArgumentException("BinLeft()出错,请检查参数!");
- }
- }
- /// <summary>
- /// E语言功能:取字节集数据
- /// </summary>
- /// <param name="data"></param>
- /// <param name="dataType"></param>
- /// <param name="index"></param>
- /// <returns></returns>
- public static object GetBinElement(this byte[] data,Type dataType,int index)
- {
- object resObj = null;
- if (dataType.isSimpleDataType())
- {
- do
- {
- var byteList = new List<byte>();
-
- if (dataType == typeof(string))
- {
- var tmp=data.Copy().Tail(data.Length - index);
- resObj = Encoding.Default.GetString(tmp.ToArray()); break;
- }
- } while (false);
- return resObj;
- }
- else
- {
- throw new ArgumentException("只有简单数据类型(非构造类型)才可以'取字节集数据'");
- }
- }
- /// <summary>
- /// E语言功能:到字节集()
- /// <para>仅限于简单数据类型转为字节集</para>
- /// <para>异常:ArgumentException</para>
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="data"></param>
- /// <returns></returns>
- public static byte[] toBin<T>(T data)
- {
- var res = new byte[] { };
- var t1 = data.GetType();
- var size = ConvertHelper.getVarSizeOf(data);
- if (size > 0)
- {
- do
- {
- if (t1 == typeof(string))
- {
- res = Encoding.Default.GetBytes(data.ToString()); break;
- }
- var s = ConvertHelper.ConvertBase(data, 10, 16);
- var tmp = new List<string>();
- var sb1 = new StringBuilder();
- int js = 0;
- foreach (var m in s)
- {
- sb1.Append(m);
- if (++js == 2) { js = 0; tmp.Add(sb1.ToString()); sb1.Clear(); }
- }
- var tmp2 = tmp.ToList().ConvertAll(s1 => ConvertHelper.HexToByte(s1));
- tmp2.Reverse();
- res = tmp2.ToArray();
- } while (false);
- }
- else
- {
- throw new ArgumentException("只有简单数据类型(非构造类型)才可以转为字节集byte[]");
- }
- return res;
- }
- }
复制代码 另附上几个用到的扩展方法:
- /// <summary>
- /// 判定Type是不是简单数据型
- /// </summary>
- /// <param name="dataType"></param>
- /// <returns></returns>
- public static bool isSimpleDataType(this Type dataType)
- {
- var dAry = new List<Type>() { typeof(string), typeof(int),typeof(long),typeof(short),typeof(ushort),
- typeof(bool),typeof(float), typeof(double),typeof(UInt16),typeof(UInt32),typeof(UInt64),typeof(decimal) };
- foreach(var m in dAry)
- {
- if (dataType == m) return true;
- }
- return false;
- }
复制代码
- public static byte[] Replace1(this byte[] sourceByteArray, byte[] oldValue, byte[] newValue)
- {
- //创建新数据多出1字节
- int newArrayLen = (int)((newValue.Length / (double)oldValue.Length) * sourceByteArray.Length) + 1;
- //得到数组长度
- newArrayLen = Math.Max(newArrayLen, sourceByteArray.Length);
- //新的最后结果数组
- byte[] newByteArray = new byte[newArrayLen];
- //新数组的当前索引
- int curIndex = 0;
- //开始结束
- int start = -1;
- int end = -1;
- //当前查找到的索引
- int oldCurindex = 0;
- //替换数据替换
- for (int x = 0; x < sourceByteArray.Length; x++)
- {
- newByteArray[curIndex] = sourceByteArray[x];
- curIndex++;
- //查找要替换的数据
- if (sourceByteArray[x] == oldValue[oldCurindex])
- {
- if (oldCurindex == 0)
- {
- start = x;
- }
- if (oldCurindex == oldValue.Length - 1)
- {
- end = x;
- oldCurindex = 0;
- }
- else
- {
- oldCurindex++;
- }
- }
- else
- {
- oldCurindex = 0;
- }
- //数据查找完成
- if (start != -1 && end != -1)
- {
- if (curIndex > 0)
- {
- curIndex -= 1;
- }
- curIndex -= end - start;
- //复制替换数据
- Buffer.BlockCopy(newValue, 0, newByteArray, curIndex, newValue.Length);
- //计算新数组的偏移量
- curIndex += newValue.Length;
- //重新设置需要复制索引的索引
- start = end = -1;
- }
- }
- //处理返回结果
- byte[] result = null;
- if (curIndex != 0)
- {
- result = new byte[curIndex];
- Buffer.BlockCopy(newByteArray, 0, result, 0, result.Length);
- }
- else
- {
- result = new byte[sourceByteArray.Length];
- Buffer.BlockCopy(sourceByteArray, 0, result, 0, result.Length);
- }
- return result;
- }
复制代码
|
|