Problems with two-dimensional array

Hey there!

I have a problem with two-dimensional arrays and maybe someone is able to give me a clue about what is going on... What I am trying to achieve here is that I have an array of longs of which every entry has to be stored into a byte array.

This is my code so far:

// just some random numbers
long myValues[4] = {2147483647, 647, 1483, 10};

// this is the two-dimensional array
unsigned char byteArray[4][4];


void setup()
{
  Serial.begin(115200);
}


void loop()
{
  // just print the values of the array once
  for (int i = 0; i < 4; i++)
  {
    Serial.print(myValues[i]);
    Serial.print(" | ");
  }
  Serial.println();

  // run through the rows
  for (int i = 0; i < 4; i++)
  {
    // run through the columns
    for (int j = 0; j < 4; j++)
    {
      byteArray[i][j] = (int)((myValues[j] >> 24) & 0xFF) ;
      byteArray[i][j] = (int)((myValues[j] >> 16) & 0xFF) ;
      byteArray[i][j] = (int)((myValues[j] >> 8) & 0XFF);
      byteArray[i][j] = (int)((myValues[j] & 0XFF));
    }
  }

  // print out the results
  for (int i = 0; i < 4; i++)
  {
    for (int j = 0; j < 4; j++)
    {
      Serial.print(byteArray[i][j], HEX);
      Serial.print(" ");
    }
    Serial.println();
  }
  
  delay(1000);
}

Edit: Okay... I guess what I should be doing is something like

      byteArray[i][0] = (int)((myValues[j] >> 24) & 0xFF) ;
      byteArray[i][1] = (int)((myValues[j] >> 16) & 0xFF) ;
      byteArray[i][2] = (int)((myValues[j] >> 8) & 0XFF);
      byteArray[i][3] = (int)((myValues[j] & 0XFF));

but the problem persists. :frowning:

      byteArray[i][j] = (int)((myValues[j] >> 24) & 0xFF) ;
      byteArray[i][j] = (int)((myValues[j] >> 16) & 0xFF) ;
      byteArray[i][j] = (int)((myValues[j] >> 8) & 0XFF);
      byteArray[i][j] = (int)((myValues[j] & 0XFF));

You assign the same value to all the columns. That can't be what you mean to do.

Oh boy... seems to work now. I was so focused on needing two loops to do the job that I couldn't realize that only one loop was all I needed:

  for (int i = 0; i < 4; i++)
  {
    byteArray[i][0] = (int)((myValues[i] >> 24) & 0xFF) ;
    byteArray[i][1] = (int)((myValues[i] >> 16) & 0xFF) ;
    byteArray[i][2] = (int)((myValues[i] >> 8) & 0XFF);
    byteArray[i][3] = (int)((myValues[i] & 0XFF));
  }

Both, the index for my rows and the index for the values happen to be the same and the column index can be hardcoded. Maybe not the most elegant solution but that's fine for me right now.

You could use two loops, if you recognize that the amount you shift the value by is a function of the inner loop index.