Convert String to int

Hi!

I have the following problem

void receiveEvent(int howMany){
  String receiveString;
  while (Wire.available() > 0){
    char code = Wire.read();
    receiveString += code;
  }
  Serial.println(receiveString);

}

So now, i want to converti receiveString into an int. But int(receiveString) doesnt work "invalid cast from String to int"
I already found this topic, but he didnt give the answear to his problem, its just "solved" :0

I believe that thread does in fact answer the question. Did you read it?

Yes, I did :slight_smile:

First I tried to use

int val4 = (int)val3;

That didnt work for me.

I also tried using the atoi() fuction

int myint = atoi(receiveString)

didnt work either :frowning:

So you didn't really read it then. The atoi() function takes a null-terminated char array (or C type string) as a parameter, not a String object.

You need to extract the character array from the String object, using the toCharArray() method, and then use atoi() (ascii to integer) function to convert the array of characters to an int.

Hmpf :-/

I get "no matching function" when I use

  char text[] = receiveString.toCharArray();

So, I theoretical I have to do the following

String -> Chararray -> int

But how do I put this into Java code? :-/

Java?

So, I theoretical I have to do the following

String -> Chararray -> int

Or get rid of the String, and use a char array to start with, making sure it's null-terminated.

tsaG:
But how do I put this into Java code? :-/

Why do you want to do that?

I get "no matching function"

Because you aren't using String.toCharArray() correctly. It doesn't return anything. See toCharArray() - Arduino Reference.

Because you aren't using String.toCharArray() correctly. It doesn't return anything. See toCharArray() - Arduino Reference.

Or just use the String::toInt() function. Geez, some people's kids...

Sorry Paul. Just following your advice -

You need to extract the character array from the String object, using the toCharArray() method, and then use atoi() (ascii to integer) function to convert the array of characters to an int.

  • from the linked thread.

tsaG:

void receiveEvent(int howMany){

String receiveString;
  while (Wire.available() > 0){
    char code = Wire.read();
    receiveString += code;
  }
  Serial.println(receiveString);

}

receiveEvent is, in effect, an interrupt service routine. You should not be attempting to do serial prints from it.

Also I advise against using the String class, I don't care how widely it is promoted.

If you collect the incoming data into a char buffer, you can use atoi to convert it into an integer.

I'm not sure if the String class is thread-safe. I doubt it. Using it in an ISR is asking for trouble.

I'm not sure if the String class is thread-safe.

Given that the Arduino has only a single thread, how is that a concern?

Ah, Paul. The interrupt service routine, and the main thread. Those two things.

Ah, Paul. The interrupt service routine, and the main thread. Those two things.

But, only one of them is running at a time. Only one can be attempting to modify the String instance at a time.

Now, there may well be issues with atomic access to the internal data that should be addressed if the String class is to be used inside an interrupt handler.

Since those issues potentially (and, in reality do) exist, using String in an ISR is probably not a good idea. Not there ever using it is all that great an idea.

PaulS:
But, only one of them is running at a time. Only one can be attempting to modify the String instance at a time.

Indeed. As would occur on any single-processor system.

However we would need to be confident that attempting to do a malloc inside an ISR would not corrupt a malloc that is being attempted outside an ISR ...

PaulS:
Since those issues potentially (and, in reality do) exist, using String in an ISR is probably not a good idea. Not there ever using it is all that great an idea.

As usual, we are in agreement. :wink:

int convert (string x) {
  int n = 0;
  char c= " ";
  for (int i=0;x.length;i++){
    c = x.charAt(i);
    n = n*10;
    n = n +int(c)
  }
return n;
}

or simple n= int(string); wont work ??

When we are talking about the String class, why are you dragging in the string class? It has nothing to do with the discussion at hand.

or simple n= int(string); wont work ??

No!