sending a 32 bit integer over I2C with 2 arduinos

I have connected 2 arduinos to send I2C data to one another for a clock project, and I have one of them driving the 4 digit 7 segment display. I want to make a function for the master one (the one without a display) to send a single integer to the other one. Here is the sending function that I have so far:

void display(int data) {
  char dataOut[4];
  dataOut[3] = (data >> 24) & 0xFF;
  dataOut[2] = (data >> 16) & 0xFF;
  dataOut[1] = (data >> 8) & 0xFF;
  dataOut[0] = data & 0xFF;
  Wire.beginTransmission(8);
  Wire.write(dataOut, 4);
  Wire.endTransmission();
}

I have tried converting the integer into a 4 byte array and sending that object using Wire.write(), and on the receiver end, I keep ending up with zeros. Here is the receiver-end function:

void receiveEvent(int bytes) {
  int data;
  while(Wire.available() > 0) {
    data += Wire.read();
    data = (data << 8);
  }
  Serial.println(data); //send to serial monitor for testing purposes
}

It should also help to notice that I am a noob with bit shifting, and these are just some things I found in some forums and I tried to combine my knowledge. Sorry if it's just a simple mistake.

Try this:

void loop()
{
   if(flag == true)
   {
      long Ldata = (long)data[3]<<24 | (long)data[2]<<16 | (long)data[1]<<8 | (long)data[0];
      Serial.println(Ldata, HEX); //send to serial monitor for testing purposes
      flag =  false;
   }
}

void receiveEvent(int bytes) 
{
   for(int i=0; i<bytes; i++)
   {
      data[i] = Wire.read();  //declare byte data[4]; as global variable; getting lower byte first
   }
   flag =  true;                 //do not execute print() method in interrupt context
}

I ended up fixing my code by converting it to a string, instead of converting it to a byte array. I do appreciate your time for helping me with what I asked for.

feyen206:
I ended up fixing my code by converting it to a string, instead of converting it to a byte array. I do appreciate your time for helping me with what I asked for.

Are you doing the print job in the loop() function?