0

I have a byte array which contains almost 10Kb data. I have to take the data from the same array in multiple of 230 bytes at a time. I tried different methods but not able to achieve the target. Can someone please help me out in this?

I have tried with below code but not serving my purpose.

public static string splitArray(byte[] array, int size)
{

    byte[] myBytes = array; // original byte array
    string[] dataArray = new string[230];

    string dataArray = null;
    for (var i = 0; i < (float)myBytes.Length / size; i++)
    {
        dataArray = myBytes.Skip(i * size).Take(size).ToString();
        Logger.Info($"Length:{dataArray.Length}");
    }

    return dataArray;
}
6
  • What methods have you tried? If you're using C# 7.2 or newer, could you use Span<byte>? Commented Apr 5, 2021 at 9:31
  • What have you tried? Post some code! Commented Apr 5, 2021 at 9:33
  • public static string splitArray(byte[] array, int size) { byte[] myBytes = array; // original byte array string[] dataArray = new string[230]; string dataArray = null; for (var i = 0; i < (float)myBytes.Length / size; i++) { dataArray = myBytes.Skip(i * size).Take(size).ToString(); Logger.Info($"Length:{dataArray.Length}"); } return dataArray; } Commented Apr 5, 2021 at 9:45
  • Would something like this work? sharplab.io/… Commented Apr 5, 2021 at 9:51
  • skip and take is not a good idea for this because those are based on an IEnumerable. You need to use Array.Copy: learn.microsoft.com/en-us/dotnet/api/… And specify the start and end index to copy from one array to another Commented Apr 5, 2021 at 13:50

1 Answer 1

1

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);
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you Dmitry, i could split the complete array into smaller chunks using your code, but i am not sure how i can use each chunck for further processing. I have to send each of the chunck to other function, also i need to add one more byte (0th byte) into the every chunck before sending it to another function. Can you please help me out?
This is not the best way to split an array of bytes of large size. You should use a buffer technique either with a MemorySpan, ArrayCopy, MemoryStream or other similar technique
@Jonathan Alfaro: in case of array of large size I'll try get rid of splitting at all and regesign MyFunc method.
Thank you Dmitry! Jonathan, our byte size would go atmost upto 20kb not more than that. Dmitry shall i go with the provided solution then?
@Ambar: why don't you try and see?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.