I2C Wire.read char* just not working

Hi All,

I have a I2C master / slave setup with onReceive and onRequest events correctly set up.

I have int values that my slave is reading from an sd card and transmitting to the master. The int is being converted to a char* and sent to the master for evaluation.

I have tested the following on the mater to test the conversion of the int to char and back again and it all works as expected

        int i = 70540;
        char returnChar[6];
        String str = String(i);
        str.toCharArray(returnChar, 6);
        
        String recon = "";  
        for(int x=0; x<6; x++){
            recon += String(returnChar[x] - '0');
        }
        Serial.println(recon);}

However when I use the following over I2C it all goes pear shaped.

Master

   Wire.requestFrom(Slave, 6);
   String recon = "";
    if(Wire.available() == 6)    { 
      for(int i = 0; i<6; i++){
        char c = Wire.read();   
        recon += String(c - '0');    
      }      
    }    
    Serial.println(recon);

Slave

void requestEvent(){
   char  returnChar[6];
   int i = 1234;
   String str = String(i);        
   str.toCharArray(returnChar,6);
   Wire.write(returnChar);
}

All I get from the above is an empty array filled with “-49” or “ÿ” depending how I cast the result on the master.
I have tried sending the result as a byte* and various methods of converting the int to char* but nothing seems to work.

Am I missing something obvious here ?

Any Help appreciated.

The I2C communication is mostly used with binary data. Are you sure you want to transmit strings ?
Just transmit the integer as two bytes.
The size of the arrays are 6, that is very small, what about a string zero terminator ?
In the slave, you do this: Wire.write(returnChar);
But perhaps you should do this: Wire.write(returnChar,6);

Using the String class inside the requestEvent is not a good idea. The requestEvent should be as small as possible and should not delay, not use the Serial port, not allocate memory and so on.

Ahh most excellent. Because the SD card would store the values as strings I got so blinded by sending a string that I overlooked the most obvious solution.

Thanks so much for pointing me in the right direction.

So what I have now working beautifully is

Master

int SlaveResponse()
{
    int response = -1;
    Wire.requestFrom(Slave, 2);
    byte barr[2];
    if(Wire.available() == 2)  
    { 
      for(int i = 0; i<2; i++){
        byte b = Wire.read();   
        barr[i] = b;    
      }      
    }    
    response = barr[1] * 256 + barr[0]; 
    return response;
}

Slave

void requestEvent(){
   int i = 32760;   
   byte barr[2] = { lowByte(i), highByte(i)};   
   Wire.write(barr,2);
}

thanks again