首頁  >  文章  >  後端開發  >  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