Click here to Skip to main content
14,539,171 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have a byte data array of 128, which means from byte 0 to 127. However, I need to code it in such a way that it reads only 16 different data from the array each time, which means in the total of 8 loops. How do I go about doing this?

What I have tried:

byte [] dat = new byte[128];
string display;
int loop = dat.length() / 16;

for(int i = 0; i < 16; i++)
{
   //display it
   display = dat[i].toString();
}
Posted
Updated 2 days ago
Rate this:
Please Sign up or sign in to vote.

Solution 1

You need an outer loop or counter that causes the inner loop to repeat. Something like:
byte [] dat = new byte[128];
string display;

for (int offset = 0; offset < dat.Length; offset += 16)
{
    for(int i = 0; i < 16; i++)
    {
       //display it
       display = dat[i + offset].ToString();
    }
    //
    // add extra code here at the end of each set of 16
    //
}
   
v3
Comments
George Swan 2 days ago
   
Good answer 5ed
Richard MacCutchan 2 days ago
   
Thanks. I just copied OP's code and forgot to desk check it.
Rate this:
Please Sign up or sign in to vote.

Solution 2

Richard's answer is the best for your needs. It may be easier on the memory to use an enumerable if you have a lot of data to process. This example uses Linq to group the data into sets each containing 16 values. It uses an IEnumerable<int> as it's easy to initialise. I'm not suggesting that you use it but it may be useful in other similar applications


static void Main(string[] args)
  {
       var x = Enumerable.Range(0, 128);
      int s = -1;
      var sets = x.Select((v, i) =>
      (Set: (i % 16 == 0) ? ++s : s, Value: v))
      .GroupBy(a => (a.Set), (key, values) => (key, values));

      foreach (var (key, values) in sets)
      {
          Console.WriteLine(key);
          string spacer = string.Empty;
          foreach (var (Set, Value) in values)
          {
              Console.Write($"{spacer}{Value}");
              spacer = spacer == string.Empty ? ", " : spacer;
          }
          Console.WriteLine();
      }

      Console.ReadLine();
  }
   
Comments
pohcb_sonic 2 days ago
   
can I ask, what's the "i" and "v"?
George Swan 2 days ago
   
'i' is the index value and 'v' is the value at that index. So for an enumerable 1,5,10 if i=1, v is 5
The Select method results in an enumerable of value tuples (int Set, int Value). The set variable is increased every time i mod 16 is 0. A Group consists of a key and an enumerable of values that belong to that key. The GroupBy method groups using the Set variable as the key into an enumerable of value tuples each consisting of a key and an enumerable of values that have that key.The first foreach loop outputs the key and the second loop outputs the values associated with that key.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100