c# – 使用另一个字节列表数组计算字节列表数组中的出现次数

c# – 使用另一个字节列表数组计算字节列表数组中的出现次数,第1张

概述我试图计算在另一个字节序列中字节序列发生的所有时间.但是,如果已经计算了它们,则无法重复使用字节.例如给定字符串 k.k.k.k.k.k.假设字节序列是k.k,那么它只会发现3次而不是5次,因为它们会像以下那样被分解:[k.k].[k.k].[k.k].并不喜欢[k.[k].[k].[k].[k] .k]他们在一圈并且基本上只是向右移动2. 理想情况下,我们的想法是了解压缩字典或运行时编码的外观. 我试图计算在另一个字节序列中字节序列发生的所有时间.但是,如果已经计算了它们,则无法重复使用字节.例如给定字符串
k.k.k.k.k.k.假设字节序列是k.k,那么它只会发现3次而不是5次,因为它们会像以下那样被分解:[k.k].[k.k].[k.k].并不喜欢[k.[k].[k].[k].[k] .k]他们在一圈并且基本上只是向右移动2.

理想情况下,我们的想法是了解压缩字典或运行时编码的外观.所以目标就是获得

k.k.k.k.k.k.只有2个部分,因为(k.k.k.)是你可以拥有的最大和最好的符号.

到目前为止这是源:

using System;using System.Collections.Generic;using System.Collections;using System.linq;using System.Text;using System.IO;    static class Compression     {        static int Main(string[] args)        {            List<byte> bytes = file.ReadAllBytes("ok.txt").ToList();            List<List<int>> List = new List<List<int>>();            // Starting Numbers of bytes - This can be changed manually.            int StartingNumBytes = bytes.Count;            for (int i = StartingNumBytes; i > 0; i--)            {                Console.Writeline("i: " + i);                for (int ii = 0; ii < bytes.Count - i; ii++)                {                    Console.Writeline("ii: " + i);                    // New pattern comes with refresh data.                    List<byte> pattern = new List<byte>();                    for (int iii = 0; iii < i; iii++)                    {                        pattern.Add(bytes[ii + iii]);                    }                    displayBinary(bytes,"red");                    displayBinary(pattern,"green");                    int matches = 0;                   // foreach (var position in bytes.ToArray().Locate(pattern.ToArray()))                    for (int position = 0; position < bytes.Count; position++) {                        if (pattern.Count > (bytes.Count - position))                        {                            continue;                        }                        for (int iiii = 0; iiii < pattern.Count; iiii++)                        {                            if (bytes[position + iiii] != pattern[iiii])                            {                                //Have to use goto because C# doesn't support continue <level>                                goto outer;                            }                        }                        // If it made it this far,it has found a match.                        matches++;                        Console.Writeline("Matches: " + matches + " Orig Count: " + bytes.Count + " POS: " + position);                        if (matches > 1)                        {                            int numBytesToRemove = pattern.Count;                            for (int ra = 0; ra < numBytesToRemove; ra++)                            {                                // Remove it at the position it was found at,once it                                // deletes the first one,the List will shift left and you'll need to be here again.                                bytes.RemoveAt(position);                            }                            displayBinary(bytes,"red");                            Console.Writeline(pattern.Count + " Bytes removed.");                            // Since you deleted some bytes,set the position less because you will need to redo the pos.                            position = position - 1;                        }                        outer:                            continue;                    }                    List<int> subList = new List<int>();                    subList.Add(matches);                    subList.Add(pattern.Count);                    // Some sort of calculation to determine how good the symbol was                    subList.Add(bytes.Count-((matches * pattern.Count)-matches));                    List.Add(subList);                }            }            display(List);            Console.Read();            return 0;        }        static voID displayBinary(List<byte> bytes,string color="white")        {            switch(color){                case "green":                    Console.Foregroundcolor = Consolecolor.Green;                    break;                case "red":                    Console.Foregroundcolor = Consolecolor.Red;                    break;                default:                    break;            }            for (int i=0; i<bytes.Count; i++)            {                if (i % 8 ==0)                    Console.Writeline();                Console.Write(GetIntBinaryString(bytes[i]) + " ");            }            Console.Writeline();            Console.resetcolor();        }        static string GetIntBinaryString(int n)        {            char[] b = new char[8];            int pos = 7;            int i = 0;            while (i < 8)            {                if ((n & (1 << i)) != 0)                {                    b[pos] = '1';                }                else                {                    b[pos] = '0';                }                pos--;                i++;            }            //return new string(b).Trimstart('0');            return new string(b);        }        static voID display(List<List<int>> List)        {            //            // display everything in the List.            //            Console.Writeline("Elements:");            foreach (var subList in List)            {                foreach (var value in subList)                {                    Console.Write("{0,4}",value);                }                Console.Writeline();            }            //            // display total count.            //            int count = 0;            foreach (var subList in List)            {                count += subList.Count;            }            Console.Writeline("Count:");            Console.Writeline(count);        }        static public int SearchBytePattern(byte[] pattern,byte[] bytes)        {            int matches = 0;            // precomputing this shaves some seconds from the loop execution            int maxloop = bytes.Length - pattern.Length;            for (int i = 0; i < maxloop; i++)            {                if (pattern[0] == bytes[i])                {                    bool ismatch = true;                    for (int j = 1; j < pattern.Length; j++)                    {                        if (bytes[i + j] != pattern[j])                        {                            ismatch = false;                            break;                        }                    }                    if (ismatch)                    {                        matches++;                        i += pattern.Length - 1;                    }                }            }            return matches;        }    }

请参考帖子获取文件的非二进制文件,这里是二进制数据:
011010110010111001101011001011100110101100101110011010110010111001101011001011100110101100101110我希望它比它的起点小.

解决方法
private static int CountOccurences(byte[] target,byte[] pattern){    var targetString = BitConverter.ToString(target);    var patternString = BitConverter.ToString(pattern);    return new Regex(patternString).Matches(targetString).Count;}
总结

以上是内存溢出为你收集整理的c# – 使用另一个字节列表/数组计算字节列表/数组中的出现次数全部内容,希望文章能够帮你解决c# – 使用另一个字节列表/数组计算字节列表/数组中的出现次数所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1247931.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存