差异行压缩算法(C#实现)
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#实现)的更多相关文章
- [原创]首次制作JQueryUI插件-Timeline时间轴
特点: 1. 支持多左右滚动,左右拖动. 2. 时间轴可上下两种显示方式. 3. 支持两种模式的平滑滚动/拖动. 4. 行压缩(后续版本此处可设置是否开启,上传的代码不带这个功能). 5. 支持hov ...
- Beyond Compare 2
Beyond Compare 2 确实很好用,差异行不交叉,自动留出空白,比windiff要清楚.
- 如何为Linux生成和打上patch
通过diff工具生成补丁, patch工具打上补丁. 在使用diff之前, 你需要保留一份未修改过的源码, 然后在其它地方修改源码的一份拷贝. diff对比这两份源码生成patch. 修改过的源码必须 ...
- Linux下生成patch和打patch
转自:http://blog.csdn.net/dl0914791011/article/details/17299103 通过diff工具生成补丁, patch工具打上补丁. 在使用diff之前, ...
- Vim文本编辑器 指令簿(二)
常常处理文本以及常常须要写代码的人,都会有自己比較常常使用的编辑器,本人喜欢用Vim.理由就是Vim编辑器灵活,而且能够达到纯键盘操作,使用纯熟情况下,根本不须要鼠标操作.听起来是不是非常酷的?只是别 ...
- 17 个 Linux 下用于 C/C++ 的最好的 IDE
C++,一个众所周知的 C 语言的扩展,是一个优秀的.强大的.通用编程语言,它能够提供现代化的.通用的编程功能,可以用于开发包括视频游戏.搜索引擎.其他计算机软件乃至操作系统等在内的各种大型应用. C ...
- MySQL--如何快速对比数据
在MySQL运维中,研发同事想对比下两个不同实例上的数据并找出差异,除主键外还需要对比每一个字段,如何做呢? 第一种方案,写程序将两个实例上的每一行数据取出来进行对比,理论可行,但是对比时间较长. 第 ...
- Python实现C代码统计工具(三)
目录 Python实现C代码统计工具(三) 声明 一. 性能分析 1.1 分析单条语句 1.2 分析代码片段 1.3 分析整个模块 二. 制作exe Python实现C代码统计工具(三) 标签: Py ...
- linux打patch简单示例
在项目中,有些模块是开源的,没有源码或者不能改动源码,想要修复.优化里面的Bug,这时就需要用到patch了. 1. 生成patch 制作补丁有两种法法,diff和quilt. 1.1 di ...
- MySQL —— 如何快速对比数据?
我们在MySql中想要对比下两个不同的实例上的数据并且找出差异,除了主键之外我们还要对比每一个字段,应该怎么做呢? 方案一:写一个程序将两个实例里面的每一行数据都分别取出来对比,但是耗时我们无法估计, ...
随机推荐
- go网络编程(一)
[B站最深度的Golang学习到实战 up主强力推荐] https://www.bilibili.com/video/BV1TK4y1a7ex/?p=101&share_source=copy ...
- Jenkins快速入门部署+实践
安装 方法一 Jenkins中文网下载jenkins.war 方法二 直接从http://mirrors.jenkins-ci.org/war/latest/jenkins.war下载最新的war包, ...
- 解密Prompt系列11. 小模型也能COT-先天不足后天来补
前两章我们分别介绍了COT的多种使用方法以及COT的影响因素.这一章更多面向应用,既现实场景中考虑成本和推理延时,大家还是希望能用6B的模型就不用100B的大模型.但是在思维链基础和进阶玩法中反复提到 ...
- 分布式存储系统举例剖析(elasticsearch,kafka,redis-cluster)
1. 概述 对于分布式系统,人们首先对现实中的分布式系统进行高层抽象,然后做出各种假设,发展了诸如CAP, FLP 等理论,提出了很多一致性模型,Paxos 是其中最璀璨的明珠.我们对分布式系统的时序 ...
- Windows校验文件MD5和SHA值的方法
1.需求背景 下载或传输文件后,需要计算文件的MD5.SHA256等校验值,以确保下载或传输后的文件和源文件一致 2.校验方法 如上图所示,可以使用Windows自带的certutil命令来计算一个文 ...
- ceph分布式存储软件pgs inconsistent
Ceph是一个开源的分布式存储系统,它提供了高性能.高可靠性以及高扩展性.Ceph的设计理念是基于对象存储模型,通过将数据分割成多个对象并存储在不同的节点上,实现数据的分布式存储和访问. Ceph的核 ...
- 如何实现IP话机接入交换机?
组网图形 简介 如果语音设备支持LLDP协议,并且支持通过network-policy TLV字段获取语音VLAN,可以在交换机上配置命令lldp tlv-enable med-tlv network ...
- Java NIO 图解 Netty 服务端启动的过程
一.启动概述 了解整体Netty常用的核心组件后,并且对比了传统IO模式.在对比过程中,找到了传统IO对应Netty中是如何实现的.最后我们了解到在netty中常用的那些组件. 本文在了解下这些核心组 ...
- API接口的对接流程和注意事项
API接口的对接流程和注意事项 随着互联网技术的发展和数字化时代的到来,API接口已经成为应用程序之间进行数据交换和通信的重要方式.API即应用程序接口,是一种定义.调用和交互的规范,使得不同应用 ...
- 后浪搞的在线版 Windows 12「GitHub 热点速览」
本周比较火的莫过于 3 位初中生开源的 Windows 12 网页版,虽然项目完成度不如在线版的 Windows 11,但是不妨一看.除了后生可畏的 win12 之外,开源不到一周的 open-int ...