首页 > 代码库 > C# 二进制文件操作(内容搜索、数据截取)

C# 二进制文件操作(内容搜索、数据截取)

        private void button2_Click(object sender, EventArgs e)        {            var list = new List<Frame>();            byte[] buf;            byte[] bytStart = {0x70, 0x6D, 0x6A, 0x73, 0x7A, 0x6A, 0x66, 0x72, 0x6D};            byte[] bytEnd = { 0x70, 0x66, 0x72, 0x61, 0x6D, 0x65, 0x65, 0x6E, 0x64 };            using (var fs = new FileStream(@"D:\avi\6.lxf",FileMode.Open))            {                buf = new byte[fs.Length];                var len = (int)fs.Length;                fs.Read(buf, 0, len);            }            var fStart = 0;            var fEnd = 0;            var offset = 0;            do            {                fStart = IndexOf(buf, bytStart, offset);                fEnd = IndexOf(buf, bytEnd, fStart);                Debug.Print("{0} ~ {1}",fStart,fEnd);                if (fStart == -1||fEnd==-1)                {                    offset = buf.Length;                }                else                {                    var frame = new Frame                    {                        Start = fStart + bytStart.Length,                        End = fEnd,                    };                    frame.Length = frame.End - frame.Start;                    frame.Data = http://www.mamicode.com/new byte[frame.Length];>                    //内容截取
Array.Copy(buf, frame.Start, frame.Data, 0, frame.Length); list.Add(frame); offset = fEnd; offset += bytEnd.Length; } } while (offset < buf.Length); }

  

查找字符串的方法

        /// <summary>        /// 报告指定的 System.Byte[] 在此实例中的第一个匹配项的索引。        /// </summary>        /// <param name="srcBytes">被执行查找的 System.Byte[]。</param>        /// <param name="searchBytes">要查找的 System.Byte[]。</param>        /// <returns>如果找到该字节数组,则为 searchBytes 的索引位置;如果未找到该字节数组,则为 -1。如果 searchBytes 为 null 或者长度为0,则返回值为 -1。</returns>        internal int IndexOf(byte[] srcBytes, byte[] searchBytes,int offset = 0)        {            if (offset == -1) { return -1; }            if (srcBytes == null) { return -1; }            if (searchBytes == null) { return -1; }            if (srcBytes.Length == 0) { return -1; }            if (searchBytes.Length == 0) { return -1; }            if (srcBytes.Length < searchBytes.Length) { return -1; }            for (var i = offset; i < srcBytes.Length - searchBytes.Length; i++)            {                if (srcBytes[i] != searchBytes[0]) continue;                if (searchBytes.Length == 1) { return i; }                var flag = true;                for (var j = 1; j < searchBytes.Length; j++)                {                    if (srcBytes[i + j] != searchBytes[j])                    {                        flag = false;                        break;                    }                }                if (flag) { return i; }            }            return -1;        }

实本类

    public class Frame    {        public int Start { get; set; }        public int End { get; set; }        public int Length { get; set; }        public byte[] Data { get; set; }    }

 

C# 二进制文件操作(内容搜索、数据截取)