Sending integer (sensor reading) over I2C

hello everybody, I am trying to send a sensor reading from one arduino to another using I2C, the reading is presented as an integer. I am planning to make the master work as a reader and the slave as sender, and connected all the wires correctly but cant seem to find a way to transfer an integer to byte and onec again to integer on the other side. can anyone please help me modify this code to get it working properly.

// Wire Master Reader
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Reads data from an I2C/TWI slave device
// Refer to the "Wire Slave Sender" example for use with this

// Created 29 March 2006

// This example code is in the public domain.


#include <Wire.h>

void setup()
{
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
}

void loop()
{
  Wire.requestFrom(2, 6);    // request 4 bytes from slave device #2

  while(Wire.available())    // slave may send less than requested
  {
    char c = Wire.read(); // receive a byte as character
    Serial.print(c);         // print the character
  }

  delay(500);
}
// Wire Slave Sender
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Sends data as an I2C/TWI slave device
// Refer to the "Wire Master Reader" example for use with this

// Created 29 March 2006

// This example code is in the public domain.


#include <Wire.h>

void setup()
{
  Wire.begin(2);                // join i2c bus with address #2
  Wire.onRequest(requestEvent); // register event
}

void loop()
{
  delay(100);
}

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent()
{
  Wire.write("hello "); // respond with message of 6 bytes
                       // as expected by master
}

convert a 16-bit int to a byte? Like this?

int sixteenbitint=1234;
byte highbyte=sixteenbitint>>8; //shift right 8 bits, leaving only the 8 high bits. 
byte lowbyte=sixteenbitint&0xFF; //bitwise AND with 0xFF

int newsixteenbitint=(highbyte<<8)+lowbyte; //make those bytes back into an int
//parenthesize everything when doing bitwise operators, the order of operations is counter-intuitive.

Make sure you’re properly dealing with negativen umbers if those are potentially encountered in your use case (if they’re not, should probably use unsigned int to enforce that) - I don’t know off the top of my head whether that will munge the sign or not.

A pretty good video on using I2C with the Arduino is:

Can you please tell me how should this code be changed if the integer had varying digits, because this is a sensor value and could vary from single digit to 3 or 4 digits?

It would be fairly unusual for an I2C to return anything other than binary values which are fixed lengths

thats clear, but can you please show me what code should I use to receive those bytes ??

I tested this code on the same arduino and converted it back to integer and it worked. but I cant seem to find a way to receive it properly on the other arduino! any help please?

byte highbyte=sixteenbitint>>8; //shift right 8 bits, leaving only the 8 high bits.
byte lowbyte=sixteenbitint&0xFF; //bitwise AND with 0xFF

int newsixteenbitint=(highbyte<<8)+lowbyte; //make those bytes back into an int

Here is a link to a tutorial and some test code which sends and receives an integer using i2c between two arduinos.

http://jamesreubenknowles.com/arduino-i2c-1680