C#如何截取二進制字節數組

 /// 截取字節數組
        /// </summary>
        /// <param name="srcBytes">要截取的字節數組</param>
        /// <param name="startIndex">開始截取位置的索引</param>
        /// <param name="length">要截取的字節長度</param>
        /// <returns>截取後的字節數組</returns>
        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;
        }
 
    }

這個是在做項目的時候真正用到的,在次做記錄以備不時之需

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章