Go Down

Topic: How can I convert a string (of numbers) to an Integer-value? (Read 1 time) previous topic - next topic


I'm slowly getting my neck around sending a .txt file to the Arduino, and have got as far as receiving e.g. "G1 X345 Y6789"

Now, I can decode as far as if it's a G, do something; if it's an X, do something else etc.

But I'm receiving X345 as a string and I want to abstract the "345" string into an integer number that I can use.

It appears that atoi() will only work char-by-char  i.e. x = atoi(datastring[1]) etc.

The string may be 1-character, up to 6-characters long, so I would like a clean, simple string-to-number conversion.

What can you suggest, please?



"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.


http://en.wiktionary.org/wiki/magagna <-- My last name.  Pretty apt.

James C4S

It appears that atoi() will only work char-by-char  i.e. x = atoi(datastring[1]) etc.

Yes well atoi() only works if you only send it a single character.  ;)

If you look at how atoi() is defined, you'll see it can take a character string:
int atoi(const char *str);
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com


Some servo test code that converts a string into an integer.

Code: [Select]

// zoomkat 11-27-10 serial servo test
// type servo position 0 to 180 in serial monitor
// for writeMicroseconds, use a value like 1500
// for IDE 0019 and later
// Powering a servo from the arduino usually DOES NOT WORK.

String readstring; //string to be captured from serial port
#include <Servo.h>
Servo myservo;  // create servo object to control a servo

void setup() {
  myservo.attach(7);  //the pin for the servo control
  Serial.println("servo-test-21"); // so I can keep track of what is loaded

void loop() {

  while (Serial.available()) {
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readstring += c; //makes readstring from the single bytes

  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

Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0


You can easily add characters to an exiting string then use atoi to translate that into an integer

String number;
long value;

Number = ""; //do this to erase previous data from string before next one starts

Number += char(serial.read()); // do this x many times depending how long the number is, assuming the serial is ready

Value = atoi(number); //after you have the full string to put into a long

That should work fine, I didn't test it but it seems about right, the char() around serial.read may not be neccesary, but im not sure


If you are using a String object, you will need to use toCharArray to get the text into a null terminated array of chars (c string) if you want to use atoi. Once you have that, or if you're already using a char buffer, just use atoi pointing to the appropriate startpoint. In your example, getting the X value needs atoi(&mybuf[4]); Of course, you'll need to figure out the offset for each number you're trying to extract as you parse the text.


Thanks to all of you.  Zoomkat's code did the trick for me.

In my futile attempts, I was missing
"char carray[readstring.length() + 1]; //determine size of the array"

and the compiler complained something wicked.

So I wrote my own function to return an int, which worked, sort-of, some of the time.

Using zoomkat's code, it works ALL of the time.  Many thanks.


Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131