Click here to Skip to main content
15,868,164 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

C#
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 23-May-20 1:36am

You need an outer loop or counter that causes the inner loop to repeat. Something like:
C#
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
    //
}
 
Share this answer
 
v3
Comments
George Swan 23-May-20 7:09am    
Good answer 5ed
Richard MacCutchan 23-May-20 7:44am    
Thanks. I just copied OP's code and forgot to desk check it.

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


C#
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();
  }
 
Share this answer
 
Comments
pohcb_sonic 23-May-20 9:36am    
can I ask, what's the "i" and "v"?
George Swan 23-May-20 10:20am    
'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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900