/// <summary> /// 截取字節數組 /// </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; }
URL:http://www.bianceng.cn/Programming/csharp/201410/45781.htm