Conversion String en Int

Bonjour,
Je récupère à partir d'un serveur NTP un String formattedDate contenant les informations suivantes :
2018-05-28T16:00:13Z
Toutes les extractions de substring que je veux faire pour l'affichage fonctionnent, mais pour des besoins de calcul, il faut que je convertisse les années, mois et jour en int.
Je n'arrive pas à convertir
formattedDate.substring(0,4)
en integer.
J'ai dû louper un épisode de la saga.

Tu trouveras ici la liste des méthodes associées à la classe String : celle que tu cherches est toInt()
annee = formattedDate.substring(0,4).toInt();

String maStr ="2018-05-28T16:00:13Z";
String formattedDate;
void setup() {
 Serial.begin(9600);
 formattedDate = maStr.substring(0,4);
 Serial.print((formattedDate.toInt()) + 1);
}

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

}

@lesept a été plus rapide que moi :

String maStr ="2018-05-28T16:00:13Z";
String formattedDate;
void setup() {
 Serial.begin(9600);
 formattedDate = maStr.substring(0,4);
 Serial.print(formattedDate.substring(0,4).toInt() + 1);
}

void loop() {}

Attention à la classe String cependant
Bonne journée.

Merci @lesept et @philippe86220
Mon cerveau était dans une boucle :
for(;;)

Oui sur ESP32 les problèmes sont moindres :wink:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.