It seems, you want something like this:
Code: (let's generalize the promblem)
public static IEnumerable<T[]> SplitArray<T>(IEnumerable<T> source, int size) {
if (null == source)
throw new ArgumentNullException(nameof(source));
if (size <= 0)
throw new ArgumentOutOfRangeException(nameof(size));
List<T> list = new List<T>(size);
foreach (T item in source) {
list.Add(item);
if (list.Count >= size) {
yield return list.ToArray();
list.Clear();
}
}
// Do we have last incomplete chunk?
if (list.Count > 0)
yield return list.ToArray();
}
Demo:
byte[] data = new byte[] { 10, 20, 30, 40, 50, 60, 70 };
string report = string.Join(Environment.NewLine, SplitArray(data, 3)
.Select(line => "{" + string.Join(", ", line) + "}"));
Console.Write(report);
Outcome:
{10, 20, 30}
{40, 50, 60}
{70}
Note, that the last chunk can be shorter then required length (3)
Edit: If you want to append 0 to each array, you can do it in the splitting method:
public static IEnumerable<T[]> MySplitArray<T>(IEnumerable<T> source, int size) {
if (null == source)
throw new ArgumentNullException(nameof(source));
if (size <= 0)
throw new ArgumentOutOfRangeException(nameof(size));
List<T> list = new List<T>(size + 1);
foreach (T item in source) {
list.Add(item);
if (list.Count >= size) {
list.Add(default(T));
yield return list.ToArray();
list.Clear();
}
}
// Do we have last incomplete chunk?
if (list.Count > 0) {
list.Add(default(T));
yield return list.ToArray();
}
}
Then you can use the rountine in a loop:
byte[] data = ...
foreach(byte[] chunk in MySplitArray(data, 230)) {
//TODO: add required code here
MyFunc(chunk);
}
Span<byte>?