Freaking strange thing happening with this simple code

String s="5";
 int i=0;
void setup() {
 

}

void loop() {
i=s.toInt();
Serial.println(i);
i++;
}

It's compiling but not executing.
There's no output.
I've searched everywhere. This is the right syntax.

Also, is there any other way for converting string to integer in arduino?
And please don't say using objects or stoi.. I've tried them.. Not working..
Not even that Int32 thing.. Don't know what it is.. I copied an example from net but it didn't execute either.

Please help.

There's no output.

There's no call to Serial.begin(), either. Coincidence? I don't think so.

Oh yes.. It's working now.. But is there any other simple way of converting a string to integer?? I'm doing in a code which is sort of a patent thing so I'm not able to share it.. And none of the ways given on stack overflow and anywhere else are working..

Do you need to convert a string (lowercase s) or a String (uppercase S) ?

If the latter then could you use the former then use atoi() to do the conversion ?

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

...R

Do you need to convert a string (lowercase s) or a String (uppercase S) ?

If the latter then could you use the former then use atoi() to do the conversion ?

I need to use the String (uppercase S) and I just tried using the string (lowercase S), it is giving an error -
‘string’ is not a datatype.

Just use cstrings - char arrays terminated with 0.

To use cstrings do we need a library? I guess String.h?

To use cstrings do we need a library?

No library needed, at least no need to explicitly #include a library.

Try this

char acstring[] = {"Hello World"};
char anotherone[] = "123";

void setup()
{
  Serial.begin(115200);
  Serial.println(acstring);
  int anint = atoi(anotherone);
  Serial.println(anint * 2);
}

void loop()
{
}

It's giving opposite question marks. :frowning:

udbhav95:
It's giving opposite question marks. :frowning:

Why take an int and convert it to a string at all.

print() the int

Check the baud rate in the serial monitor.

  Serial.begin(115200);Change the baud rate in either the code or the Serial monitor so that they match

Now it works but this still doesn’t solve my problem… Bcoz you see I cannot initialise a char array with a string… I tried doing it in these two ways but it gave an error… I need to convert a string variable into an integer… Not a char array into an integer…

char acstring[] = {"Hello World"};
String s="123";
char anotherone[] = s;

void setup()
{
  Serial.begin(9600);
  Serial.println(acstring);
  int anint = atoi(anotherone);
  Serial.println(anint * 2);
}

void loop()
{
}
char acstring[] = {"Hello World"};
String s="2";
int l=s.length();
char[l] anotherone;
anotherone[] = s;

void setup()
{
  Serial.begin(9600);
  Serial.println(acstring);
  int anint = atoi(anotherone);
  Serial.println(anint * 2);
}

void loop()
{
}

I also tried to break up the string into digits and then make a number out of them in an int variable separately but didn’t work either… Bcoz I had to use the toInt() function in it… Which is the problem itself… In this code s3 is the string that has a number I need to convert in the integer.

int sl=s3.length();
                int g=0,h=1;
                String str="";
                for(int k=0;k<=sl;k++)
                {
                  char ch=s3.charAt(k);
                  str+=ch;
                  int l=str.toInt();
                  g=g+10*(l/h);
                  h*=10;
                  str="";
                }
                Serial.println("g: "+g);*/

In this code s3 is the string

int sl=s3.length();

A string has no "length()" method.
A string has no methods at all.

Please try again.
Use real words (e.g. not "bcoz") and capitals where capitals are needed.

udbhav95:

String s="5";

int i=0;
void setup() {

}

void loop() {
i=s.toInt();
Serial.println(i);
i++;
}




It's compiling but not executing.
There's no output.
I've searched everywhere. This is the right syntax.

Also, is there any other way for converting string to integer in arduino?
And please don't say using objects or stoi.. I've tried them.. Not working..
Not even that Int32 thing.. Don't know what it is.. I copied an example from net but it didn't execute either.

Please help.

Will this help?
http://www.cplusplus.com/reference/string/stoi/

udbhav95:
I need to convert a string variable into an integer.. Not a char array into an integer..

String.toInt()

void setup() {
  Serial.begin(9600);
  String myString = "123";
  Serial.print(myString.toInt());
}

void loop() {

}

@CtrlAltElite

Check this out

String s="Udbhav";
int l;
void setup()
{
  Serial.begin(9600);
  l=s.length();
  Serial.println(l*2);
}

void loop()
{
}

Bcoz I think that you were still thinking that string has no methods, bcoz may be.. I don't know.. You are just starting with the codes.. Or bcoz you are Jon Snow.. Work it up kiddo.. Bcoz you know why? Bcoz this code I just sent you?.. Is running.

@232 Okay, let me see
@BulldogLowell Okay, I'm checking it

232:
Will this help?
stoi - C++ Reference

I did as it said in the example. It gives an error - "stoi was not declared in this scope"

String s="Udbhav";
String sz;
int l;
void setup()
{
  Serial.begin(9600);
  l=stoi(s,&sz,10);
  Serial.println(l+s.substring(sz));
}

void loop()
{
}

@BulldogLowel - The code you sent does run, but my program is too big and toInt() function is not able to execute in it I don't know why..

but my program is too big and toInt() function is not able to execute in it I don't know why..

Until you post your code, neither do we.

CtrlAltElite:
Until you post your code, neither do we.

Okay, so this is the code:

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
 
IPAddress ip(192,168,137,200); //IP address for your arduino.

char server[] = "192.168.137.1"; //IP address of your computer.

int interrupt=0; //Variable to control the iterations of void loop().

String Name="Udbhav"; //Variable to be written on the database.
int Roll=51;
int Age=17;

String rcv=""; //Variable in which the server response is recorded.

EthernetClient client;

void setup()
{
  
  Serial.begin(9600);
  
  Ethernet.begin(mac, ip);
  
  delay(5000); //Wait for ethernet to connect.

}

void httpRequest()
{
  if (client.connect(server, 81)) 
  {
    Serial.println("Connection established 1");
    client.print(String("GET ") + "/tryjson.php/" + " HTTP/1.1\r\n" + "Host: " + server + "\r\n" + "Connection: close\r\n\r\n"); //GET request for server response.
    unsigned long timeout = millis();
    while (client.available() == 0) 
    {
      if (millis() - timeout > 25000) //If nothing is available on server for 25 seconds, close the connection.
      { 
        return;
      }
    }
    while(client.available())
    {
      String line = client.readStringUntil('\r'); //Read the server response line by line..
      rcv+=line; //And store it in rcv.
    }
    client.stop(); // Close the connection.
  }
  else
  {
    Serial.println("Connection failed 1");
  }
  //Serial.println("Received string: ");
  //Serial.println(rcv); //Display the server response.
  //Serial.println();
  
  String s2=rcv.substring((rcv.indexOf('[')),rcv.indexOf(']')); // Extract the line returned by JSON object.

  int l=s2.length();
  String s="",s3;
  int m,i=0;
  while(i<l)
  {
    m=s2.indexOf('"',i);
    if(m!=-1)
    {
      for(int j=m+1;j<l;j++)
      {
        if(s2.charAt(j)!='"')
        {
          s+=s2.charAt(j);
        }
        else
        {
          i=j;
          break;
        }
      }
      Serial.println(s); //Display the exact extracted values.
      s3=s;
      s="";
    }
    i++;
  }
  Serial.println("s3: "+s3);
  int conv=s3.toInt();
  Serial.println("Conv: "+conv);
}

void loop() 
{
  if(interrupt==0)
  {
      httpRequest();
      delay(1000);
      if (client.connect(server, 81)) 
      {
      Serial.println("Connection Established 2");
      client.print("GET /info.php?"); //GET request to write data to the database.
      client.print("Name=");
      client.print(Name);
      client.print("&Roll=");
      client.print(Roll);
      client.print("&Age=");
      client.print(Age);
      client.println(" HTTP/1.1"); 
      client.println("Host: 192.168.137.1"); 
      client.println("Connection: close"); 
      client.println(); 
      client.println(); 
      client.stop();
      }
      else
      {
        Serial.println("Connection failed 2");
      }
  }
  interrupt++;
  delay(10000);
}

And this is the output:

Connection established 1
Age
17
s3: 17
Established 2
Connection Established 2

In the second last line, it should print value of conv… Instead, it prints something which is not even supposed to show on screen…

s3 is the string variable that I need to convert in integer…

s3 is the string variable

No, s3 is a String variable - if you want to make any headway programming, you will have to start paying attention to details, and using language properly.

You'll discover that folks around here don't like Strings very much.