C#如何截取二进制字节数组

 /// 截取字节数组
        /// 
        /// 要截取的字节数组
        /// 开始截取位置的索引
        /// 要截取的字节长度
        /// 截取后的字节数组
        public byte[] SubByte(byte[] srcBytes, int startIndex, int length)
        {
            System.IO.MemoryStream bufferStream = new System.IO.MemoryStream();
            byte[] returnByte = new byte[] { };
            if (srcBytes == null) { return returnByte; }
            if (startIndex < 0) { startIndex = 0; }
            if (startIndex < srcBytes.Length)
            {
                if (length < 1 || length > srcBytes.Length - startIndex) { length = srcBytes.Length - startIndex; }
                bufferStream.Write(srcBytes, startIndex, length);
                returnByte = bufferStream.ToArray();
                bufferStream.SetLength(0);
                bufferStream.Position = 0;
            }
            bufferStream.Close();
            bufferStream.Dispose();
            return returnByte;
        }
 
    }

这个是在做项目的时候真正用到的,在次做记录以备不时之需

你可能感兴趣的:(C#,截取字节数组,c#)