Problem converting char to int.

I am using the following code to send a char array of six digits over a wireless link. When it is received at the other end I need to convert it back to a number. My problem is that the array which is 199324 is converted by atoi to 2716.

Obviously I am doing something wrong and would appreciate any help.

TX code

 /*
    * Arduino Wireless Communication Tutorial
    *     Example 1 - Transmitter Code
    *                
    * by Dejan Nedelkovski, www.HowToMechatronics.com
    * 
    * Library: TMRh20/RF24, https://github.com/tmrh20/RF24/
    */
    #include <SPI.h>
    #include <nRF24L01.h>
    #include <RF24.h>
    RF24 radio(7, 8); // CE, CSN
    const byte address[6] = "00001";

void setup() {
    radio.begin();
      radio.openWritingPipe(address);
      radio.setPALevel(RF24_PA_MIN);
      radio.stopListening();



 Serial.begin(9600); 
}

void loop() {
  // put your main code here, to run repeatedly:

   int runs = 199;
    int wkts = 3;
     int overs = 24;

   char cruns [4];
   sprintf(cruns, "%03d", runs);

    char cwkts [2];
   sprintf(cwkts, "%01d", wkts);

    char covers [3];
   sprintf(covers, "%02d", overs);

   Serial.println(cruns);
   Serial.println(cwkts);
   Serial.println(covers);

   String StringRuns = String(cruns);
   String StringWkts = String(cwkts);
   String StringOvers = String(covers);
   String Output = String (StringRuns+StringWkts+StringOvers);
  

  Serial.print ("The runs are ");
  Serial.println(StringRuns);

   Serial.print ("The wkts are ");
  Serial.println(StringWkts);
   Serial.print ("The overs are ");
  Serial.println(StringOvers);
  Serial.println(Output);

  char charBuf [Output.length()+1] ; // charBuf is the array we send by radio

  Output.toCharArray(charBuf, Output.length()+1);

  radio.write(&charBuf, sizeof(charBuf));

 
  delay(1000);

}

RX Code

/*
  Arduino Wireless Communication Tutorial
        Example 1 - Receiver Code

  by Dejan Nedelkovski, www.HowToMechatronics.com

  Library: TMRh20/RF24, https://github.com/tmrh20/RF24/
*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";
void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
}
void loop() {
  if (radio.available()) {
    char text[7] = "";
    radio.read(&text, sizeof(text));

     int digits = atoi(text);
    Serial.print ("array = "); 
    Serial.println (text);

    Serial.print ("integer = ");
  
    Serial.println (digits);

    delay (1000);

  }
}

199324 won't fit in an int. Make your digits variable a long and use atol rather than atoi.

Brilliant, Thanks for your help.

OP, you're certainly going about it the hard way when you build your charBuf. And, you're using the frowned-upon C++ String class. Consider:

  char charBuf[7];
  uint8_t runs = 199;
  uint8_t wkts = 3;
  uint8_t overs = 24;
  uint32_t packed = runs * 1000 + wkts * 100 + overs;
  (void) ltoa(packed, charBuf, 10);
  radio.write(&charBuf, strlen(charBuf)+1);

The code will be even more efficient if you send it as ASCII representation of a HEX number:

  char charBuf[9];
  byte runs = 199;
  byte wkts = 3;
  byte overs = 24;
  unsigned long packed = ((unsigned long) runs) <<16 | ((unsigned long) wkts) << 8 | (unsigned long) overs;
  (void) ltoa(packed, charBuf, 16);  
  radio.write(&charBuf, strlen(charBuf)+1);
  radio.write(&charBuf, strlen(charBuf)+1);

Why do you add one? Sending the NULL is pointless, when the other end knows how much data was sent, and can add the NULL as needed.

PaulS:

  radio.write(&charBuf, strlen(charBuf)+1);

Why do you add one? Sending the NULL is pointless, when the other end knows how much data was sent, and can add the NULL as needed.

I'd take it a step further and not convert to char[]. Send the unsigned long as binary instead, just 4 bytes.