首页  >  文章  >  后端开发  >  C#二进制字节数组操作函数 截取字节数组SubByte的示例代码

C#二进制字节数组操作函数 截取字节数组SubByte的示例代码

黄舟
黄舟原创
2017-03-13 17:46:113081浏览

C#二进制字节数组操作函数 截取字节数组SubByte

        /// 
        /// 截取字节数组
        /// 
        /// 要截取的字节数组
        /// 开始截取位置的索引
        /// 要截取的字节长度
        /// 截取后的字节数组
        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#二进制字节数组操作函数 截取字节数组SubByte的示例代码的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn