Converting a numeric string into an int

I have some code, that outputs a string containing a specific number, ranging from 0 to 2500. I want to convert it to an int. I have searched almost everywhere and I haven't found anything. I tried itoa(); but it returned the following error: cannot convert 'String' to 'const char*' for argument '1' to 'int atoi(const char*)'. Is there any specific function for it? I'm just a one step away from finishing my whole project...

Sure... use char[] instead of String and your problems are gone. :)

You can use the String::toCharArray() function to extract the char array that the String object wraps, The itoa function will then convert that array to an int.

By the way, when talking about strings, you need to be careful to distinguish between strings and Strings. Completely different animals.

Oh, thanks for reminding me. What I have is a String, not string. What's the difference between them?

A string is a NULL terminated array of characters.

A String is an instance of the String class, which encapsulates a string. It manages the allocation/deallocation of memory, and the positioning of characters in the array.

Depending on which version of the String class you are using, there may be a toInt() method that does what defining a place to extract the character array, extracting the character array, and calling itoa() with that character array does.

Thanks for the explanation. At this point, I use the toCharArray(); function to convert my String to char array. But, there is a problem.

int mw;
char mwbuffer[5];

readString.toCharArray(mwbuffer, sizeof (mwbuffer)); //Decode the string into an actual int.
mw = atoi(mwbuffer);

As previously said, the string contains bare number 0 up to bare number 2500. Everything is fine, until I convert it to int using atoi();, because it translates the number I wanted into ASCII character... So, now, I have a char array, that contains the number (for example) 1234, I want that number as a int (because I want to preform some further mathematical operations with it). How do I do that? Thanks for all your patience.

As previously said, the string contains bare number 0 up to bare number 2500.

The technical term in that statement, bare, needs some explanation.

If you define a String object like so:

String stg = "1256";

and use the toCharArray() function:

char buf[5];
stg.toCharArray(buf, 5);

The values in buf will be: buf[0] = '1' buf[1] = '2' buf[2] = '5' buf[3] = '6' buf[4] = 0

Then, call the atoi function:

int num = atoi(buf);

The value in num will be 1256.

Everything is fine, until I convert it to int using atoi();, because it translates the number I wanted into ASCII character

Again, an explanation is required. Or, better yet, post some code.

How do I do that?

That's been explained, and coded. If it isn't working, there is a problem with your code. We need to see it, and some sample input/output.

Two ways of doing the atoi I use in some servo control code, the first uses a known string length and the second finds the length if it is unknown.

  if (readString.length() >0) {
    int n;
    char carray[6];
    readString.toCharArray(carray, sizeof(carray));
    n = atoi(carray); 
  if (readstring.length() >0) {
    Serial.println(readstring);  //so you can see the captured string 
    char carray[readstring.length() + 1]; //determine size of the array
    readstring.toCharArray(carray, sizeof(carray)); //put readstringinto an array
    int n = atoi(carray); //convert the array into an Integer 
    myservo.writeMicroseconds(n); // for microseconds
    //myservo.write(n); //for degees 0-180

Sorry for the late reply guys, I was away from civilization. By atoi() mangling the string up, I meant the following scenairo: I was printing the value on a LCD: incoming string was the number 80, it got converted into a char array then atoi'ed and then lcd.print()'ed. The LCD displayed the letter P (ASCII 80) instead of the value. I'm going to try using lcd.printIn() instead of print next time. Thank you all.

EDIT: When I use printIn instead of print, I get the following error: "error: no matching function for call to 'LCD4Bit_mod::printIn(int&, int)'" and "note: candidates are: void LCD4Bit_mod::printIn(char*)" EDIT2: A quick replacement of lcd.print with Serial.print revealed that the problem is with the LCD library, not atoi. EDIT3: I figured how to use the regular, updated LiquidCrystal library instead of that buggy LCD4bit_mod, and voila, it worked!