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

本文为 testcs_dn(微wx笑) 原创文章   

转载连接: http://blog.csdn.net/testcs_dn/article/details/25277005


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

[csharp]  view plain  copy
  1. ///   
  2. /// 截取字节数组  
  3. ///   
  4. /// 要截取的字节数组  
  5. /// 开始截取位置的索引  
  6. /// 要截取的字节长度  
  7. /// 截取后的字节数组  
  8. public byte[] SubByte(byte[] srcBytes, int startIndex, int length)  
  9. {  
  10.     System.IO.MemoryStream bufferStream = new System.IO.MemoryStream();  
  11.     byte[] returnByte = new byte[] { };  
  12.     if (srcBytes == null) { return returnByte; }  
  13.     if (startIndex < 0) { startIndex = 0; }  
  14.     if (startIndex < srcBytes.Length)  
  15.     {  
  16.         if (length < 1 || length > srcBytes.Length - startIndex) { length = srcBytes.Length - startIndex; }  
  17.         bufferStream.Write(srcBytes, startIndex, length);  
  18.         returnByte = bufferStream.ToArray();  
  19.         bufferStream.SetLength(0);  
  20.         bufferStream.Position = 0;  
  21.     }  
  22.     bufferStream.Close();  
  23.     bufferStream.Dispose();  
  24.     return returnByte;  
  25. }  

你可能感兴趣的:(c#)