差异行压缩算法(C#实现)

发布时间 2023-10-26 17:32:08作者: 迷海
 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 }