上一章节,中提供了简单可以替换的例子,在本节中,进行了bug修改并且对不同方式进行了效率测试
测试用的代码
- #region 性能对比
- protected void 二进制数据替换()
- {
- string sourcestr = "68 30 55 08 67 30 55 16 33 16";
- //string oldstr = "30";
- //string newvalstr = "FA FA FA";
- string oldstr = "FA FA FA";
- string newvalstr = "30";
- byte[] source = sourcestr.Replace(" ", "").HexToByteArray();
- //"68 12 00 12 00 68 8D DE 00 00 00 00 0D 62 01 00 68 30 30 34 73 42 31 30 ED 16".Replace(" ", "").HexToByteArray();
- byte[] old = oldstr.Replace(" ", "").HexToByteArray();
- byte[] newval = newvalstr.Replace(" ", "").HexToByteArray();
- byte[] result = HexUtility.Replace(source, old, newval);
- string strs = result.ByteArray2HexString();
- var sb = strs.Replace(" ", string.Empty) == sourcestr.Replace(oldstr, newvalstr).Replace(" ", "");
- if (!sb)
- {
- var d = "替换结果不正确";
- }
- var listRep = strs == source.ReplaceA(old, newval).ByteArray2HexString();
- //全部替换
- result = HexUtility.Replace(source, new List<HexReplaceEntity>()
- {
- new HexReplaceEntity()
- {
- oldValue = "68".Replace(" ", "").HexToByteArray(),
- newValue = "67 02".Replace(" ", "").HexToByteArray(),
- }
- ,
- new HexReplaceEntity()
- {
- oldValue = "67".Replace(" ", "").HexToByteArray(),
- newValue = "67 01".Replace(" ", "").HexToByteArray(),
- },
- new HexReplaceEntity()
- {
- oldValue = "16".Replace(" ", "").HexToByteArray(),
- newValue = "67 03".Replace(" ", "").HexToByteArray(),
- },
- });
- string str2 = result.ByteArray2HexString();
- //字符串替换检查
- if (str2.Replace(" ", string.Empty) != sourcestr
- .Replace("67", "67 01")
- .Replace("68", "67 02")
- .Replace("16", "67 03")
- .Replace(" ", string.Empty)
- )
- {
- var d1 = "替换结果不正确";
- }
- string restx = HexUtility.Replace(result, "67 01".Replace(" ", "").HexToByteArray(), "67".Replace(" ", "").HexToByteArray()).ByteArray2HexString();
- result = HexUtility.Replace(result, new List<HexReplaceEntity>()
- {
- new HexReplaceEntity()
- {
- oldValue = "67 02".Replace(" ", "").HexToByteArray(),
- newValue = "68".Replace(" ", "").HexToByteArray(),
- }
- ,
- new HexReplaceEntity()
- {
- oldValue = "67 01".Replace(" ", "").HexToByteArray(),
- newValue = "67".Replace(" ", "").HexToByteArray(),
- },
- new HexReplaceEntity()
- {
- oldValue = "67 03".Replace(" ", "").HexToByteArray(),
- newValue = "16".Replace(" ", "").HexToByteArray(),
- },
- });
- string rel = result.ByteArray2HexString();
- //字符串替换检查
- if (rel.Replace(" ", string.Empty) != sourcestr
- .Replace("67 01", "67")
- .Replace("67 02", "68")
- .Replace("67 03", "16")
- .Replace(" ", string.Empty)
- )
- {
- var d2 = "替换结果不正确";
- }
- }
- protected void 二进制数据替换数组复制与List效率对比()
- {
- string sourcestr = "68 30 55 08 67 30 55 16 33 16 68 39 00 39 00 68 C2 DE 00 00 00 00 0C 6D 08 00 01 04 29 00 00 01 00 01 09 04 00 19 44 00 00 02 00 14 03 11 18 24 25 00 01 01 11 42 46 01 48 43 03 01 95 99 00 00 00 00 00 00 00 00 00 00 28 C7 16 68 39 00 39 00 68 C2 DE 00 00 00 00 0C 6D 08 00 01 04 29 00 00 01 00 01 09 04 00 19 44 00 00 02 00 14 03 11 18 24 25 00 01 01 11 42 46 01 48 43 03 01 95 99 00 00 00 00 00 00 00 00 00 00 28 C7 16 68 39 00 39 00 68 C2 DE 00 00 00 00 0C 6D 08 00 01 04 29 00 00 01 00 01 09 04 00 19 44 00 00 02 00 14 03 11 18 24 25 00 01 01 11 42 46 01 48 43 03 01 95 99 00 00 00 00 00 00 00 00 00 00 28 C7 16";
- string oldstr = "30";
- string newvalstr = "FA FA FA";
- //string oldstr = "FA FA FA";
- //string newvalstr = "30";
- byte[] source = sourcestr.Replace(" ", "").HexToByteArray();
- //"68 12 00 12 00 68 8D DE 00 00 00 00 0D 62 01 00 68 30 30 34 73 42 31 30 ED 16".Replace(" ", "").HexToByteArray();
- byte[] old = oldstr.Replace(" ", "").HexToByteArray();
- byte[] newval = newvalstr.Replace(" ", "").HexToByteArray();
- //100万次计算
- var arr = System.Linq.Enumerable.Range(1, 1000000);
- //方法1
- Action act = () => { foreach (var k in arr) { source.Replace(old, newval); } };
- //方法2
- Action act2 = () => { foreach (var k in arr) { source.ReplaceA(old, newval); } };
- //array 用时1828
- System.Diagnostics.Debug.WriteLine("array 用时" + Stopwatch(act));
- //list 用时2481
- System.Diagnostics.Debug.WriteLine("list 用时" + Stopwatch(act2));
- }
- /// <summary>
- /// 测试运行时间
- /// </summary>
- /// <param name="method">需要执行的方法</param>
- /// <param name="summary">对方法的说明</param>
- /// <returns>返回,这个方法执行用了多少毫秒</returns>
- public static long Stopwatch(Action method)
- {
- System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
- watch.Restart();
- //运行方法
- method.Invoke();
- watch.Stop();
- return watch.ElapsedMilliseconds;
- }
- /// <summary>
- /// 测试运行时间
- /// </summary>
- /// <param name="method">需要执行的方法</param>
- /// <param name="obj">参数对像</param>
- /// <returns>返回,这个方法执行用了多少毫秒</returns>
- public static long Stopwatch<T>(Action<T> method, T obj)
- {
- return Stopwatch(new Action(() =>
- {
- method.Invoke(obj);
- }));
- }
- #endregion
hextobytearray() 是效字符串转换成为二进制数据
替换核心代码
- /// <summary>
- /// 二进制数据 操作,对单个数组替换时,如果新值长度小于旧值长度时出现的问题进行了修改
- /// </summary>
- public static class HexUtility
- {
- /// <summary>
- /// 二进制替换,如果没有替换则返回原数组对像的复本.比使用List效率高25%
- /// </summary>
- /// <param name="sourceByteArray">源数据</param>
- /// <param name="oldValue">需要替换的数据</param>
- /// <param name="newValue">将要替换成为的数据</param>
- public static byte[] Replace(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;
- }
- /// <summary>
- /// 二进制替换,如果没有替换则返回原数组对像的复本.
- /// </summary>
- /// <param name="sourceByteArray">源数据</param>
- /// <param name="replaces">需要替换的数据集合</param>
- public static byte[] Replace(this byte[] sourceByteArray, List<HexReplaceEntity> replaces)
- {
- //创建新数据多出1字节
- int newArrayLen = (int)((replaces.Sum(p => p.newValue.Length) / (double)replaces.Sum(p => p.oldValue.Length)) * sourceByteArray.Length) + 1;
- //得到数组长度
- newArrayLen = Math.Max(newArrayLen, sourceByteArray.Length);
- //新的最后结果数组
- byte[] newByteArray = new byte[newArrayLen];
- //新数组的当前索引
- int curIndex = 0;
- bool find = false;
- //替换数据替换
- for (int x = 0; x < sourceByteArray.Length; x++)
- {
- foreach (HexReplaceEntity rep in replaces)
- {
- //查找要替换的数据
- if (sourceByteArray[x] == rep.oldValue[rep.oldCurindex])
- {
- if (rep.oldCurindex == 0)
- {
- rep.start = x;
- }
- if (rep.oldCurindex == rep.oldValue.Length - 1)
- {
- rep.end = x;
- rep.oldCurindex = 0;
- }
- else
- {
- rep.oldCurindex++;
- }
- }
- else
- {
- rep.oldCurindex = 0;
- newByteArray[curIndex] = sourceByteArray[x];
- find = false;
- }
- //数据查找完成
- if (rep.start != -1 && rep.end != -1)
- {
- find = true;
- if (rep.newValue.Length >= rep.oldValue.Length)
- {
- //复制替换数据
- Buffer.BlockCopy(rep.newValue, 0, newByteArray, curIndex, rep.newValue.Length);
- //计算新数组的偏移量
- curIndex += rep.newValue.Length;
- }
- else
- //由大字节替换为少字节时出现了问题
- {
- curIndex -= rep.end - rep.start;
- //复制替换数据
- Buffer.BlockCopy(rep.newValue, 0, newByteArray, curIndex, rep.newValue.Length);
- //计算新数组的偏移量
- curIndex += rep.newValue.Length;
- }
- //重新设置需要复制索引的索引
- rep.start = rep.end = -1;
- break;
- }
- }
- if (!find)
- {
- curIndex++;
- }
- }
- //处理返回结果
- 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;
- }
- /// <summary>
- /// 二进制替换,如果没有替换则返回原数组对像的复本. 内部使用List,比直接用数组替换效率低25%
- /// </summary>
- /// <param name="sourceByteArray">源数据</param>
- ///
- /// </summary>
- /// <param name="oldValue">需要替换的数据</param>
- /// <param name="newValue">将要替换成为的数据</param>
- public static byte[] ReplaceA(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);
- int oldCurindex = 0;
- List<byte> result = new List<byte>(newArrayLen);
- //替换数据替换
- for (int x = 0; x < sourceByteArray.Length; x++)
- {
- var b = sourceByteArray[x];
- result.Add(b);
- if (b == oldValue[oldCurindex])
- {
- if (oldCurindex == oldValue.Length - 1)
- {
- oldCurindex = 0;
- //称除现有数据
- for (int k = 0; k < oldValue.Length; k++)
- {
- result.RemoveAt(result.Count - 1);
- }
- //添加新数据
- result.AddRange(newValue);
- }
- else
- {
- oldCurindex++;
- }
- }
- }
- byte[] resultarr = result.ToArray();
- result.Clear();
- result = null;
- return resultarr;
- }
- }
- /// <summary>
- /// 替换用的数据实体
- /// </summary>
- public class HexReplaceEntity
- {
- /// <summary>
- /// 需要替换的原始值
- /// </summary>
- public byte[] oldValue { get; set; }
- /// <summary>
- /// 新值
- /// </summary>
- public byte[] newValue { get; set; }
- /// <summary>
- /// 默认开始结束标记
- /// </summary>
- internal int start = -1;
- /// <summary>
- /// 默认开始结束标记
- /// </summary>
- internal int end = -1;
- //当前查找到的索引
- internal int oldCurindex = 0;
- }