Pages: [1]   Go Down
Author Topic: How can I convert a string (of numbers) to an Integer-value?  (Read 816 times)
0 Members and 1 Guest are viewing this topic.
Aranyaprathet, Thailand
Offline Offline
Jr. Member
**
Karma: 0
Posts: 68
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?

Geoff
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26618
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

atoi works on true C null-terminated strings.
Logged

"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.

Southern California
Offline Offline
God Member
*****
Karma: 5
Posts: 539
I like blinky lights
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

How about sscanf() ?
Logged

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

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.  smiley-wink

If you look at how atoi() is defined, you'll see it can take a character string:
int atoi(const char *str);
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

0
Offline Offline
Tesla Member
***
Karma: 147
Posts: 9741
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
// 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="";
  }
}

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Offline Offline
Edison Member
*
Karma: 5
Posts: 1730
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 72
Posts: 3754
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Aranyaprathet, Thailand
Offline Offline
Jr. Member
**
Karma: 0
Posts: 68
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: [1]   Go Up
Jump to: