C#去除byte数组头尾杂质(即不需要的数据)

代码如下:

        /// <summary>
        /// 去除byte数组头尾杂质(即不需要的数据)
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            //---------------------去除头杂质---------------------------------
            byte[] listTemp = new byte[] 
            {   
				1, 2, 3, 4, 5, 6, 7, 8, 0x55, 0xaa, 6, 6, 6, 6, 6, 6, 6, 7, 7, 
                7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9,7,8,8,
                1, 4, 3, 2, 4, 6, 7, 8, 9, 2, 1, 0x55, 0xaa, 1, 2, 3, 4, 6 ,2
            };

            int index = 0;
            while (true)
            {
                if (listTemp[index] != 0x55 && listTemp[index++] != 0xaa)
                    index++;
                else break;
            }
            byte[] listNew = new byte[listTemp.Length - index];
            Array.Copy(listTemp, index, listNew, 0, listTemp.Length - index);

            int count = 10;
            List<byte> finaly = new List<byte>();
            int lastStart = listNew.ToList<byte>().LastIndexOf(0x55);
            if (listNew[lastStart + 1] == 0xaa)
            {
                if ((listNew.Length) - (lastStart + 1) != count)
                {
                    byte[] listFinaly = new byte[lastStart];          //listFinaly 此处数组就是剔除了头尾杂质的
                    Array.Copy(listNew, 0, listFinaly, 0, lastStart);
                    finaly = listFinaly.ToList<byte>();
                }
            }
            Console.ReadLine();
        }



原文地址:https://www.cnblogs.com/myesn/p/5601601.html