Invalid cast from type ‘String’ to type ‘int’

Hi !
I need some help, because I can't understand where is the problem, why I can't convert String to int ?

Part of code :

int octetReception=0;
char caractereRecu=0;
String chaineReception="";
int compt=0;
...

void loop() {

chaineReception="";
// RECEPTION
while (Serial.available()>0) {

        octetReception=Serial.read();
        compt=compt+1;
 

        if (octetReception==10) {
          compt=0;
          break;
        }
        else { 
           caractereRecu=char(octetReception);
           chaineReception=chaineReception+caractereRecu;
         }
       }


// TRAITEMENT


  if (tr1==true) {
        tr1=false;
        pos1=int(chaineReception);}


  else if(tr2==true) {
        tr2=false;
        pos2=int(chaineReception);}

...

The error message is : :relaxed:
BCI_v2.ino: In function ‘void loop()’:
BCI_v2.ino:112:33: error: invalid cast from type ‘String’ to type ‘int’
BCI_v2.ino:117:33: error: invalid cast from type ‘String’ to type ‘int’

Thx !

Tirmit:
why I can't convert String to int ?

You can if you do it properly.

Look at the available functions.

Ok,
I found a solution, I don't know if it is the best thing to do, but it works :roll_eyes: :

...

if (tr1==true & deblock==true) {
        tr1=false;
        deblock=false;
        pos1=String(chaineReception[0]).toInt()*100 + String(chaineReception[1]).toInt()*10 + String(chaineReception[2]).toInt();

      }


  else if(tr2==true & deblock==true) {
        tr2=false;
        deblock=false;  
        pos2=String(chaineReception[0]).toInt()*100 + String(chaineReception[1]).toInt()*10 + String(chaineReception[2]).toInt();
  
    }

...

thx for help

        pos1=String(chaineReception[0]).toInt()*100 + String(chaineReception[1]).toInt()*10 + String(chaineReception[2]).toInt();

toInt() can handle more than a single character String. The above link has an example of it doing just that.

Ok, thx :slight_smile:
I don't know why, but before toInt() didn't work.