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. 在Linux环境下通过命令行执行JMeter脚本后查看响应结果的配置

    在Linux环境中进行性能测试时,我们可能会遇到一定程度的报错.如果无法打开JMeter的GUI界面,但又需要查看响应结果,可以按照以下步骤进行配置: 1. 打开JMeter的安装目录,在`bin/` ...

  2. NoSuchMethodError: Closure call with mismatched arguments:

    原因:某个方法的参数中,回调函数写的有问题,

  3. 【websocket】小白快速上手flask-socketio

    大家好,我是一个初级的Python开发工程师.本文是结合官方教程和代码案例,简单说下我对flask-socketio的使用理解. 一.websocket简介 websocket 说白一点就是,建立客户 ...

  4. 数据库连接池之c3p0-0.9.1.2,16年的古董,发生连接泄露怎么查(一)

    背景 这篇文章是写给有缘人的,为什么这么说呢,因为本篇主要讲讲数据库连接池之c3p0-0.9.1.2版本. 年轻的朋友,可能没怎么听过c3p0了,或者也仅限于听说,这都很正常,因为c3p0算是200几 ...

  5. Codeforces Round #881 (Div. 3) A-F

    比赛链接 A 代码 #include <bits/stdc++.h> using namespace std; using ll = long long; int a[57]; bool ...

  6. TypeScript:Type 'boolean' is not assignable to type 'never'.

    问题原因 当我们声明一个空数组而不显示键入它并尝试向其中添加元素时,会发生该错误. 解决方案 声明数组类型即可 参考链接 https://bobbyhadz.com/blog/typescript-a ...

  7. React: 按钮点击时修改颜色

    背景 当存在多个点击按钮时,需要提示用户点击的哪个按钮,所以要进行颜色的修改 import * as React from 'react'; import './style.css'; export ...

  8. 让nodejs开启服务更简单--express篇

    上一篇文章说到,nodejs获取客户端请求需要我们自己去处理请求参数.请求方式等,而在express框架内部集成了很多好用的方法,我们不需要从0开始编写各种处理逻辑,这样可以极大提高我们的开发效率~ ...

  9. 使用kafka自带脚本进行压力测试

    前言 kafka官方自带压力测试脚本: 消费者压力测试:kafka-consumer-perf-test.sh 生产者压力测试:kafka-producer-perf-test.sh 测试节点: 17 ...

  10. 如何实现Excel中的多级数据联动

    摘要:本文由葡萄城技术团队于博客园原创并首发.转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 前言 在类Excel表格应用中,常用的需求场景是根据单元格之间 ...