C# 二进制替换第一弹 byte 数组替换
在做通讯相关的数据操作时常常须要用到 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 数组替换的更多相关文章
- Java从内存流中读取byte数组
Java中通过servlet接收二进制数据,然后将二进制数据流读取为byte数组.开始使用:byte[] bs = new byte[request.getContentLength()];reque ...
- JS的replace默认只替换第一个匹配项
1. JS的replace默认只替换第一个匹配项. 解决方法: 使用正则表达式进行匹配替换[ ①.replace(new RegExp(②,"g") ,③); ] ①:包含 ...
- 二进制样式的字符串与byte数组互转函数示例
开发时用到的方法,记录下: /// <summary> /// 测试方法 /// </summary> private void TestFun() { Response.Wr ...
- 二进制流 最后一段数据是最后一次读取的byte数组没填满造成的
while(in.read(temp)!=-1){ out.write(temp); } 改成: int len; while((len=in.read(temp))!=-1){out.write(t ...
- .NET如何快速比较两个byte数组是否相等
目录 前言 评测方案 几种不同的方案 For循环 Memcmp 64字长优化 SIMD Sse Avx2 SequenceCompare 总结 参考文献 前言 之前在群里面有群友问过一个这样的问题,在 ...
- typecho流程原理和插件机制浅析(第一弹)
typecho流程原理和插件机制浅析(第一弹) 兜兜 393 2014年03月28日 发布 推荐 5 推荐 收藏 24 收藏,3.5k 浏览 虽然新版本0.9在多次跳票后终于发布了,在漫长的等待里始终 ...
- 我的长大app开发教程第一弹:Fragment布局
在接下来的一段时间里我会发布一个相对连续的Android教程,这个教程会讲述我是如何从零开始开发“我的长大”这个Android应用. 在开始之前,我先来介绍一下“我的长大”:这是一个校园社交app,准 ...
- Byte数组和字符串相互转换的问题
第一:需求:将文件转成byte数组,之后转成字符串返回.过滤器接收到响应内容后,需要将响应的内容转成byte数组. 第二:我刚开始的做法: Controller:byteArr = Conversio ...
- byte数组转float实现与byte转换其它类型时进行&运算原理
下面是将byte数组转换为float的实现 public static float getFloat(byte[] b) { int accum = 0; accum = accum|(b[0] &a ...
随机推荐
- 配置nginx支持thinkphp框架
因为nginx本身没有支持pathinfo,所以无法使用thinkphp框架,不过我们可以在配置里进行修改使其能够正常使用thinkphp. 1.修改配置支持pathinfo vi /etc/ngin ...
- RHEL 7特性说明(六):集群
来自:Linux中国 2014-07-16 00:00:00 ed Hat Enterprise Linux 7.0 是 Red Hat 的下一代操作系统完整套件,旨在用于关键任务企业级计算以及顶 ...
- 2016022613 - redis连接命令集合
redis连接命令 1.ping 用途:检查服务器是否正在运行 返回数据pong,表示服务器在运行. 2.quit 用途:关掉当前服务器连接 3.auth password 用途:服务器验证密码 没有 ...
- 一个消除if语句的例子
// 一个按钮点击事件,判断点击按钮是那一个显示出他的信息 - (IBAction)buttonPressed:(id)sender { if (sender == self.leftButton) ...
- NEURAL NETWORKS, PART 2: THE NEURON
NEURAL NETWORKS, PART 2: THE NEURON A neuron is a very basic classifier. It takes a number of input ...
- iOS获取键盘的高度
- 改变UITextField placeHolder色彩、字体
改变UITextField placeHolder颜色.字体 我们有时需要定制化UITextField对象的风格,可以添加许多不同的重写方法,来改变文本字段的显示行为.这些方法都会返回一个CGRect ...
- JSch - Java实现的SFTP(文件下载详解篇)(转)
上一篇讲述了使用JSch实现文件上传的功能,这一篇主要讲述一下JSch实现文件下载的功能.并介绍一些SFTP的辅助方法,如cd,ls等. 同样,JSch的文件下载也支持三种传输模式:OVERWRI ...
- 14.6.8 Configuring the InnoDB Master Thread IO Rate 配置InnoDB 主线程IO 速率:
14.6.8 Configuring the InnoDB Master Thread IO Rate 配置InnoDB 主线程IO 速率: 主线程 在InnoDB 是一个线程 执行各种任务在后台. ...
- OOD/DDP 中的 SRP 原则
单一职责原则 SRP(The Single Responsibility Principle):一个类应该只有一个发生变化的原因.这里的变化指职责的变化. SRP 很好理解,它的要求是 让一个类只做一 ...