I have a large byte array and I am splitting it into multiple byte array by 1000 bytes and process them. I am using IEnumerable and I can use foreach loop but i want to know that which number of array inside IEnumerable, I am using. I can get the total count but do not know which number of byte array I am processing, so can anyone help me to figure out how to achieve this? Please let me know if you need more details.
public static IEnumerable<byte[]> SplitSourceBytes(byte[] SrcBytes, int size) { int srcLenght = SrcBytes.Length; byte[] source = null; int i = 0; for (; srcLenght > (i + 1) * size; i++) { source = new byte[size]; Array.Copy(SrcBytes, i * size, source, 0, size); yield return source; } int sourceLeft = srcLenght - i * size; if (sourceLeft > 0) { source = new byte[sourceLeft]; Array.Copy(SrcBytes, i * size, source, 0, sourceLeft); yield return source; } }
ArraySegment<byte>, for which you have an offset where it starts on the larger array (from which you can even deduce the "index")