在做通讯相关的数据操作时常常须要用到 byte[]   byte 数组替换操作.比方协义转换相关的

如今提供了几种替换的方法

    /// <summary>/// 二进制数据 操作/// </summary>public class HexUtility{/// <summary>/// 二进制替换,假设没有替换则返回原数组对像的复本./// </summary>/// <param name="sourceByteArray">源数据</param>/// <param name="oldValue">须要替换的数据</param>/// <param name="newValue">将要替换成为的数据</param>public static byte[] Replace(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++){//查找要替换的数据if (sourceByteArray[x] == oldValue[oldCurindex]){if (oldCurindex == 0){start = x;}if (oldCurindex == oldValue.Length - 1){end = x;oldCurindex = 0;}else{oldCurindex++;}}else{oldCurindex = 0;newByteArray[curIndex] = sourceByteArray[x];curIndex++;}//数据查找完毕if (start != -1 && end != -1){//复制替换数据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(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>/// 替换数据实体/// </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;}

C# 二进制替换第一弹 byte 数组替换相关推荐

  1. php 数组字符串替换字符串,利用PHP怎么替换数组的字符串

    利用PHP怎么替换数组的字符串 发布时间:2020-12-18 15:21:42 来源:亿速云 阅读:91 作者:Leah 这期内容当中小编将会给大家带来有关利用PHP怎么替换数组的字符串,文章内容丰 ...

  2. php遍历数组替换内容,PHP使用数组依次替换字符串中匹配项

    先来看个sql语句: 想把上面这句sql的中括号表示的日期依次换成下面的数组中的元素array('2015-07-01','2015-07-15'); 用正则匹配:找到第一个中括号部分,用第一个元素替 ...

  3. R语言使用str_replace函数和str_replace_all函数替换字符串中匹配到的模式:str_replace函数替换第一个匹配到的字符串、str_replace_all函数替换所有匹配到的

    R语言使用str_replace函数和str_replace_all函数替换字符串中匹配到的模式:str_replace函数替换第一个匹配到的字符串.str_replace_all函数替换所有匹配到的 ...

  4. 指针和数组替换和区别

    指针和数组替换和区别 指针和数组在很多方面都可以替换 为什么不直接用while(*str++ != '\0')涉及到了Lvalue和Rvalue的问题  // http://www.dotcpp.co ...

  5. 二进制样式的字符串与byte数组互转函数示例

    开发时用到的方法,记录下: /// <summary>/// 测试方法 /// </summary>private void TestFun(){Response.Write( ...

  6. php 数组 批量替换字符串,php数组替换字符串

    PHP5常用函数 PHP已经更新到很多个版本,最近用的比较多的要数PHP5.下面我们为大家总结了PHP5常用函数,以便大家将来实际编写代码中查看. pathinfo返回文件路径的信息 ,包括以下的数组 ...

  7. python字符串变量替换_python字符串替换第一个字符串的方法

    Python 截取字符串使用 变量[头下标:尾下标],就可以截取相应的字符串,其中下标是从0开始算起,可以是正数或负数,下标可以为空表示取到头或尾. # 例1:字符串截取 str = '1234567 ...

  8. python 字符串替换_python字符串替换第一个字符串的方法

    Python 截取字符串使用 变量[头下标:尾下标],就可以截取相应的字符串,其中下标是从0开始算起,可以是正数或负数,下标可以为空表示取到头或尾. # 例1:字符串截取 str = '1234567 ...

  9. JS字符串替换、字符串转数组、数组过滤

    详细举例JS字符操作,包括串替换.字符串转数组.数组过滤 1.字符串替换 //替换,字符串回车字符替换成逗号 let str1="aaa\nbbb\n\n\ccc\n\nddd"; ...

最新文章

  1. linux快速上手之多服务器间路由配置
  2. 概率统计:第二章 随机变量及其分布
  3. 和同学沟通,一定是时间效率比较高的
  4. Ajax学习整理笔记
  5. sqlite3 cmd命令输出乱码
  6. Helm 3 完整教程(二十三):使用 Files 方法在模板中读取文件内容
  7. 数据预处理第1讲:标准化
  8. 投票最喜欢报表模板,赢取复联3正版玩偶
  9. C语言行列中大小判断,c语言判断两个矩阵是否相等(行列相同的矩阵)
  10. C语言俄罗斯方块代码(成功版)
  11. 软件项目开发报价指南
  12. 18个免费视频素材网站,超高清、不限速、无版权、可商用,1秒解决你90%的视频剪辑难题!
  13. LTE系统中的OFDM技术
  14. Word使用中常用的快捷键
  15. springboot项目基础骨架搭建并完成基本增删改查及多种形式分页
  16. linux 服务器共享文件客户端查看,在Linux下查看共享文件夹
  17. 设置电脑的背景颜色为保护色
  18. tomcat部署web应用及架设论坛
  19. 计算机视觉中的transformer模型创新思路总结
  20. python去除含st的股票

热门文章

  1. 转载:公司招聘中不能说的秘密
  2. docker提交容器成一个新的镜像commit和push,以及docker常用命令
  3. JMeter集合点功能的使用
  4. sort()函数与升序、降序
  5. python 做界面时如何使图片保持透明背景_Python matplotlib生成图片背景透明的示例代码...
  6. viewcube翻译_view cube是什么意思
  7. lua 差值 日期_lua时间戳和日期转换及踩坑
  8. rmi反序列化导致rce漏洞修复_企业安全05-Fastjson =1.2.47反序列化RCE漏洞(CNVD-2019-22238)...
  9. 软件质量保证计划_质量保证QA与质量控制QC
  10. python3内置函数_python3--内置函数