Go Down

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

#### geoff_p

##### Oct 11, 2011, 05:55 pm
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.

Geoff

#### AWOL

#1
##### Oct 11, 2011, 05:56 pm
atoi works on true C null-terminated strings.
"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.
I speak for myself, not Arduino.

#2

#### cmiyc

#3
##### Oct 12, 2011, 01:34 am

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

#### zoomkat

#4
##### Oct 12, 2011, 05:04 am
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() {  Serial.begin(9600);  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()) {    delay(10);      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    readstring="";  } }`
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

#### winner10920

#5
##### Oct 12, 2011, 05:49 am
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

#### wildbill

#6
##### Oct 12, 2011, 01:46 pm
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.

#### geoff_p

#7
##### Oct 12, 2011, 01:48 pm
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.

Geoff

Go Up