我有一個大的字節數組,我將它分成多個字節數組1000字節並處理它們。我正在使用IEnumerable,我可以使用foreach循環,但我想知道,我正在使用IEnumerable內的數組數。我可以得到總數,但不知道我正在處理的是哪一個字節數組,所以任何人都可以幫助我弄清楚如何實現這一點?如果您需要更多詳細信息,請告訴我。如何使用大字節數組?
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;
}
}
如果您需要知道您的索引,請使用for循環,而不是foreach。 – cadrell0
通過字節數組的數量,你是指原始數組的第i塊?您可以使用字典來識別塊。 – MGZero
你能提供循環或字典的例子嗎? – updev