Problem: String to int

Salve ragazzi, è da un paio di giorni che faccio testa e tastiera, non riesco a convertire una stringa in intero :S
Hi guys, it's been a couple of days that I make head and keyboard, I can not convert a string in integer :S

In pratica il problema è questo, un programma Java mi invia in seriale una stringa così formata "stringa-intero", leggo la stringa, cerco il carattere "-" e la divido in due sottostringhe, una contente la stringa ed una contenente il numero.
The problem is this, a Java program sends me by serial a string like "string-integer",I read the string, look for the "-" character and divide it into two substrings, one containing the string and one containing the number .

Siccome il numero deve essere dato come paramero a delay() mi serve intero, ma non riesco a convertirlo >.<
The number must be given as a parameter at delay () I need it as integer, but I can not convert it >.<

Ho provato con atoi() e con sscanf() ma mi viene restituito un numero negativo (lo controllo stampandolo a schermo con Serial.println())
I tried with atoi() and with sscanf() but it returned a negative number (checking it by screen printing it with Serial.println ())

Qualcuno di buona volontà che mi da una mano?
Can someone help me?

Inserisco sotto il codice.
This is the code.

#define serraggio 13
#define riscaldatore 2
#define salita 4
#define discesa -10
#define aspirazione 7
#define raffreddamento 8
#define sformatura 12

String x; //stringa in arrivo
String x2; //tempo
String x1; //nome operazione
int a;


int tempo;

void setup(){
pinMode (serraggio, OUTPUT);  //serraggio
pinMode (riscaldatore, OUTPUT); //riscaldatore
pinMode (salita, OUTPUT); //piano
pinMode (discesa, OUTPUT); //discesa
pinMode (aspirazione, OUTPUT); //aspirazione
pinMode (raffreddamento, OUTPUT); //raffreddamento
pinMode (sformatura, OUTPUT); //sformatura
Serial.begin(9600); //lavoro a 9600
Serial.flush();
}


void loop(){
  if(Serial.available()){   
    x= "";
    x = Serial.readString(); 
    boolean f= false;
    for(int q=0; q<= x.length(); q++){
       if(x.charAt(q)=='-') {
        f=true; //l'operazione ha un tempo
        a=q;
      }
     }  
     if(f==true){
       x1= x.substring(0, a);
       x2= x.substring(a+1, x.length()-1);
       Serial.println(x2);
       int x2l = x2.length();
       char arrayTempo[x2l+1];
       for(int r=0; r<= x2l; r++){
        arrayTempo[r] = x2.charAt(r);
        Serial.println(arrayTempo[r]);
       }       
       //sscanf(arrayTempo, "%d", &tempo); 
       //tempo = atoi(arrayTempo);
       Serial.println(tempo);
     }
  }
}

Forum inglese qui ... C'è una sezione italiana se non si legge / scrive inglese

english forum here... There is an Italian section if you don't read/write english

I would suggest you study Serial Input Basics and stop using the String class in favor of functions from the standard libraires (such as stdlib.h or string.h)

for example parsing a c-string like this

"[color=blue]123-45-218-644-228-32-1-666[/color]"

can be achieved with the strtok() function and calling this repetitively

here is some code

char cstringToParse[100];

void setup() {
  Serial.begin(115200);

  strcpy(cstringToParse, "123-45-218-644-228-32-1-666"); // to simulate receiving from wherever

  char * item = strtok(cstringToParse, "-");
  while (item != NULL) {
    Serial.println(item);
    item = strtok(NULL, "-");
  }
}

void loop() {}

that will print in the console

[sub][color=blue]123
45
218
644
228
32
1
666
[/color][/sub]

and this would look like this

Thanks for the answer J-M-L :3
But this don't help me to resolve my problem >.<

I always receive a String with this format "word-number", there aren't other numbers.
I just need to change my "number" from String to int

can you give an example for what you receive?

RiscaldatoreOn-123456

it's a similar approach

char cstringToParse[100];

void setup() {
  Serial.begin(115200);

  strcpy(cstringToParse, "RiscaldatoreOn-123456"); // to simulate receiving from wherever

  char * item = strtok(cstringToParse, "-");

  if (item != NULL) {
    Serial.print("Parsing ");
    Serial.println(item);
    // skip to the next one which should be the integer
    item = strtok(NULL, "-");
    Serial.print("Found the text \"");
    Serial.print(item);
    Serial.print("\" ");
    
    long v = atol(item);
    Serial.print("which is the integer value ");
    Serial.println(v);
    
    Serial.print("if I add one that will become ");
    Serial.println(v + 1);
  }
}

void loop() {}

will print to the Serial console

</sub> <sub>Parsing RiscaldatoreOn Found the text "123456" which is the integer value 123456 if I add one that will become 123457</sub> <sub>

Found the text "123456" which is the integer value 123456

Which is the integral value, not the integer value. The value is stored in a long.

Just being pedantic... 8)

PaulS:
Just being pedantic... 8)

please excuse my french :slight_smile:

Thank you very much J-M-L, now it works!! you are my savior ;D