private byte[] DifferenceRowOrder(int offset, int count, byte[] inbyte)//差异行命令(此处的offset和count都从1开始)
{
List<byte> result = new List<byte>();
if (offset >= 1 && offset <= 31 && count >= 1 && count <= 8)
{
offset -= 1;
count -= 1;
string order =Convert.ToString(count,2).PadLeft(3, '0') + Convert.ToString(offset,2).PadLeft(5, '0');
result.Add(Convert.ToByte(Convert.ToInt32(order, 2)));
result.AddRange(inbyte);
return result.ToArray();
}
else //大于31个字节
{
offset -= 31;
count -= 1;
string order =Convert.ToString(count,2).PadLeft(3, '0') + "11111";
result.Add(Convert.ToByte(Convert.ToInt32(order, 2)));
for (int i = 0; i < offset / 255; i++)
{
result.Add(Convert.ToByte(Convert.ToInt32("11111111", 2)));
}
offset = offset % 255;
if (offset != 0)
{
result.Add(Convert.ToByte(offset));
}
result.AddRange(inbyte);
return result.ToArray();
}
}


private List<string> DuplicateTopLine(List<byte> last, List<byte> currt)
{
List<int> indexs = new List<int>();
List<string> continuous = new List<string>();
if (last.Count != currt.Count)
{
if (last.Count < currt.Count)
{
int count = currt.Count - last.Count;
for (int i = 0; i < count; i++)
{
last.Add(0x00);
}
}
else
{
int count = last.Count - currt.Count;
for (int i = 0; i < count; i++)
{
currt.Add(0x00);
}
}
}
for (int i = 0; i < currt.Count; i++)
{
if (last[i] != currt[i])
{
if (indexs.Count>0&&indexs[indexs.Count - 1] == i - 1) //存在连续不同的索引
{
if (continuous[continuous.Count - 1].Contains("-"))
{
string []indexstring = continuous[continuous.Count - 1].Split('-');
continuous[continuous.Count - 1] = indexstring[0] + "-" + i;
}
else//上一个是单独的索引
{
continuous[continuous.Count - 1] = continuous[continuous.Count - 1] + "-" + i;
}
}
else
{
continuous.Add(i.ToString());
}
indexs.Add(i);
}
else
{
}
}
return continuous;
}

怎么调用

                 
                 //下方的line为个数为8的倍数的二进制字符串,如果不为8的倍数,请使用PadRight用0补足
                 //输出结果为lineorder,lineorder由命令字节+替换字节+命令字节+替换字节......组成
                 List<byte> lineorder = new List<byte>();
templineBytes.Clear();
for (int k = 0; k < line.Length / 8; k++)
{
templineBytes.Add(Convert.ToByte(Convert.ToInt32(line.Substring(k * 8, 8), 2)));
}
List<string> continuousIndexs= DuplicateTopLine(lasttemplineBytes, templineBytes);
int lastindex=0;//上一个字节操作位
bool isin = false;
for (int m = 0; m < continuousIndexs.Count; m++)
{
if (continuousIndexs[m].Contains("-"))//是连续的不同索引块
{
string[] num = continuousIndexs[m].Split('-');
List<byte> currt = new List<byte>();
if (Convert.ToInt32(num[1]) >= templineBytes.Count)
{
if (Convert.ToInt32(num[0]) >= templineBytes.Count)
{
for (int k=0; k< Convert.ToInt32(num[1])- Convert.ToInt32(num[0]);k++)
{
currt.Add(0x00);
}
}
else
{
currt = templineBytes.GetRange(Convert.ToInt32(num[0]), Convert.ToInt32(templineBytes.Count-1) - Convert.ToInt32(num[0])+1);
}
}
else
{
currt = templineBytes.GetRange(Convert.ToInt32(num[0]), Convert.ToInt32(num[1]) - Convert.ToInt32(num[0])+1);
}
int startIndex = Convert.ToInt32(num[0]);//开始的数组索引
int endIndex = Convert.ToInt32(num[1]);//结束的数组索引
if (currt.Count > 8)
{
//大于8个字节再分
int forcount = currt.Count / 8;
int lastfor = currt.Count % 8;
for (int k=0;k< forcount;k++)
{
//0-7
if (!isin)
{
if (k == 0)
{
lineorder.AddRange(DifferenceRowOrder(startIndex - lastindex + 1, 8, currt.GetRange(k * 8, 8).ToArray()));//使用相对偏移量
}
else
{
lineorder.AddRange(DifferenceRowOrder(1, 8, currt.GetRange(k * 8, 8).ToArray()));//使用相对偏移量
}
isin = true;
}
else
{
if (k == 0)
{
lineorder.AddRange(DifferenceRowOrder(startIndex - lastindex, 8, currt.GetRange(k * 8, 8).ToArray()));//使用相对偏移量
}
else
{
lineorder.AddRange(DifferenceRowOrder(1, 8, currt.GetRange(k * 8, 8).ToArray()));//使用相对偏移量
}
}
}
if (lastfor > 0)
{
lineorder.AddRange(DifferenceRowOrder(1, lastfor, currt.GetRange(forcount * 8, lastfor).ToArray()));//使用相对偏移量
}
lastindex = endIndex;
}
else
{
//当前连续小于8字节
if (!isin)
{
lineorder.AddRange(DifferenceRowOrder(startIndex - lastindex + 1, currt.Count, currt.ToArray()));//使用相对偏移量
isin = true;
}
else
{
lineorder.AddRange(DifferenceRowOrder(startIndex - lastindex, currt.Count, currt.ToArray()));//使用相对偏移量
}
lastindex = endIndex;
}
}
else //是单个的不同索引
{
int currtindex = Convert.ToInt32(continuousIndexs[m]);
if (currtindex >= templineBytes.Count)
{
if (currtindex - lastindex + 1 <= 0)
{
throw new Exception();
}
if (!isin)
{
lineorder.AddRange(DifferenceRowOrder(currtindex - lastindex + 1, 1, new byte[] { 0x00}));//使用相对偏移量
isin = true;
}
else
{
lineorder.AddRange(DifferenceRowOrder(currtindex - lastindex, 1, new byte[] { 0x00 }));//使用相对偏移量
}
}
else
{
if (currtindex - lastindex + 1 <= 0)
{
throw new Exception();
}
if (!isin)
{
lineorder.AddRange(DifferenceRowOrder(currtindex - lastindex + 1, 1, new byte[] { templineBytes[currtindex] }));//使用相对偏移量
isin = true;
}
else
{
lineorder.AddRange(DifferenceRowOrder(currtindex - lastindex, 1, new byte[] { templineBytes[currtindex] }));//使用相对偏移量
}
}
lastindex = currtindex;
}
}
lasttemplineBytes = templineBytes;
if (lineorder.Count != 0)
{
result.AddRange(Encoding.ASCII.GetBytes((lineorder.Count) + ""));//个数
result.Add(0x57);//W
result.AddRange(lineorder);
}
else
{
result.Add(0x1B);//ESC
result.Add(0x2A);//*
result.Add(0x62);//b
result.Add(0x31);//1
result.Add(0x59);//Y
}

差异行压缩算法(C#实现)的更多相关文章

  1. [原创]首次制作JQueryUI插件-Timeline时间轴

    特点: 1. 支持多左右滚动,左右拖动. 2. 时间轴可上下两种显示方式. 3. 支持两种模式的平滑滚动/拖动. 4. 行压缩(后续版本此处可设置是否开启,上传的代码不带这个功能). 5. 支持hov ...

  2. Beyond Compare 2

    Beyond Compare 2 确实很好用,差异行不交叉,自动留出空白,比windiff要清楚.

  3. 如何为Linux生成和打上patch

    通过diff工具生成补丁, patch工具打上补丁. 在使用diff之前, 你需要保留一份未修改过的源码, 然后在其它地方修改源码的一份拷贝. diff对比这两份源码生成patch. 修改过的源码必须 ...

  4. Linux下生成patch和打patch

    转自:http://blog.csdn.net/dl0914791011/article/details/17299103 通过diff工具生成补丁, patch工具打上补丁. 在使用diff之前, ...

  5. Vim文本编辑器 指令簿(二)

    常常处理文本以及常常须要写代码的人,都会有自己比較常常使用的编辑器,本人喜欢用Vim.理由就是Vim编辑器灵活,而且能够达到纯键盘操作,使用纯熟情况下,根本不须要鼠标操作.听起来是不是非常酷的?只是别 ...

  6. 17 个 Linux 下用于 C/C++ 的最好的 IDE

    C++,一个众所周知的 C 语言的扩展,是一个优秀的.强大的.通用编程语言,它能够提供现代化的.通用的编程功能,可以用于开发包括视频游戏.搜索引擎.其他计算机软件乃至操作系统等在内的各种大型应用. C ...

  7. MySQL--如何快速对比数据

    在MySQL运维中,研发同事想对比下两个不同实例上的数据并找出差异,除主键外还需要对比每一个字段,如何做呢? 第一种方案,写程序将两个实例上的每一行数据取出来进行对比,理论可行,但是对比时间较长. 第 ...

  8. Python实现C代码统计工具(三)

    目录 Python实现C代码统计工具(三) 声明 一. 性能分析 1.1 分析单条语句 1.2 分析代码片段 1.3 分析整个模块 二. 制作exe Python实现C代码统计工具(三) 标签: Py ...

  9. linux打patch简单示例

    在项目中,有些模块是开源的,没有源码或者不能改动源码,想要修复.优化里面的Bug,这时就需要用到patch了. 1.    生成patch 制作补丁有两种法法,diff和quilt. 1.1   di ...

  10. MySQL —— 如何快速对比数据?

    我们在MySql中想要对比下两个不同的实例上的数据并且找出差异,除了主键之外我们还要对比每一个字段,应该怎么做呢? 方案一:写一个程序将两个实例里面的每一行数据都分别取出来对比,但是耗时我们无法估计, ...

随机推荐

  1. 聊聊Spring注解@Transactional失效的那些事

    一.前言 emm,又又又踩坑啦.这次的需求主要是对逾期计算的需求任务进行优化,现有的计算任务运行时间太长了.简单描述下此次的问题:在项目中进行多个数据库执行操作时,我们期望的是将其整个封装成一个事务, ...

  2. gin 接口开发 - 用户输入自动 TrimSpace

    最近在思考一个问题,针对用户的输入,能不能快速校验? 比方说下面的 struct,大家用过 gin 的就知道,支持指定某个字段为 required,用户如果不输入,就检验不通过. type Login ...

  3. 图解 Vue 响应式原理

    Vue 初始化 模板渲染 组件渲染 为了便于理解,本文将从以下两个方面进行探索: 从 Vue 初始化,到首次渲染生成 DOM 的流程. 从 Vue 数据修改,到页面更新 DOM 的流程. Vue 初始 ...

  4. ApiPost: Error:ESOCKETTIMEDOUT

    原因 apipost设置响应时间过短 解决方案

  5. 开源流量检测引擎Dalton安装记录

    几个月之前照着官方文档安装过,一次性就成功,昨天重装了服务器再安装Dalton,怎么都安装不了 一直报错 ERROR: Service 'agent-suricata-current' failed ...

  6. 【干货】浅谈如何给.net程序加多层壳达到1+1>2的效果

    软件破解分白盒和黑盒两种方式. 白盒破解:白盒破解是指对软件进行破解时,攻击者可以访问软件的内部结构和源代码.这种破解方式通常发生在软件的开发者.技术人员或软件公司内部.攻击者使用这些详细信息来理解软 ...

  7. 从浏览器架构认识BOM和DOM

    浏览器架构 JavaScript运行在浏览器,BOM就是连接JavaScript代码和浏览器的桥梁,而DOM就是用来操作各种标签元素的. BOM包括 window.history.location.d ...

  8. springboot项目图片不显示的问题

    首先确认你的图片路径是对的,maven重新clean,install了,web服务也重启了 那么大概率就是浏览器缓存的原因,因为页面直接用的是缓存的旧数据,所以显示不出来. 在不修改浏览器设置的情况下 ...

  9. JDK中动态库加载路径问题,一文讲清

    前言 本周协助测试同事对一套测试环境进行扩容,我们扩容很原始,就是新申请一台机器,直接把jdk.resin容器(一款servlet容器).容器中web应用所在的目录,全拷贝到新机器上,servlet容 ...

  10. 领域驱动设计(DDD):三层架构到DDD架构演化

    三层架构的问题 在前文中,我从基础代码的角度探讨了如何运用领域驱动设计(DDD)来实现高内聚低耦合的代码.本篇文章将从项目架构的角度,继续探讨三层架构与DDD之间的演化过程,以及DDD如何优化架构的问 ...