How can a parsed string be converted to a an integer



How can a parsed string be converted to an integer, please look at the attached exampelcode.

Kind regards


Sting to Integer Examle.ino (897 Bytes)

The atoi() function was developed specifically to do that.

The parse example in Serial Input Basics should be useful. It uses atoi()


Very simple servo test code that converts a captured numeric value into an integer.

//zoomkat 7-30-10 serial servo test
//type servo position 0 to 180 in serial monitor
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

void setup() {
  Serial.println("servo-test"); // so I can keep track of what is loaded

void loop() {

  while (Serial.available()) {
    char c =;  //gets one byte from serial buffer
    readString += c; //makes the String readString
    delay(2);  //slow looping to allow buffer to fill with next character

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured String 
    int n = readString.toInt();  //convert readString into a number
    Serial.println(n); //so you can see the integer

Keep in mind that the String class does use more memory than a plain C string. In the following program:

void setup() {
  char input[] = "12345";
  //String input = "12345";
  int number;

  //number = input.toInt();
  number = atoi(input);

void loop() {

The code compiles to 3280 bytes. If you uncomment the String class calls and comment out the straight C string code, it compiles to 3752. While not a huge savings, almost 500 bytes sometimes is important.