Pages: [1]   Go Down
Author Topic: Parsing Data  (Read 245 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi everyone,,
I have data "123456789"
and I want to parse data that the result is:
data1=123
data2=456
data3=789

how the list program???
thank you,,
Logged

London
Offline Offline
Edison Member
*
Karma: 23
Posts: 1050
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi everyone,,
I have data "123456789"
and I want to parse data that the result is:
data1=123
data2=456
data3=789

how the list program???
thank you,,
What form is your data in? Is it a serial input (one digit at a time) or stored in a variable?
Logged

Atlanta, USA
Offline Offline
Edison Member
*
Karma: 33
Posts: 1409
AKA: Ray Burne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@budiraharjo:

If the data "123456789" is a String object as defined here: http://arduino.cc/en/Reference/StringObject then you can use any of the String object functions to parse the object.

CAVEAT: please read this first: http://forum.arduino.cc/index.php/topic,124367.0.html
The String Object is not a highly recommended approach.  That being said, if will work.

IF the data "123456789" is coming in over a port (say, serial port) then you must aggregate individual character digits (bytes) until you have all 9 digits in one string object.  IF you have control over the incoming data string format, you can take a different approach by using delimiters, that is: "123 " "456 " "789 " or "123,456,489" or a variety of similar approaches.  See my calculator code for how I parse commands and numerics: http://forum.arduino.cc/index.php?topic=172406.msg1280843#msg1280843

- Ray
Logged

London
Offline Offline
Edison Member
*
Karma: 23
Posts: 1050
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If the data is in a variable then maths can do it for you:
Code:
mydata = 123456789;
data1 = mydata/1000000;
data2 = (mydata % 1000000)/1000;
data3 = (mydata % 1000);
mydata is a long
data1, data2 and data3 are ints.
Logged

0
Offline Offline
Tesla Member
***
Karma: 114
Posts: 8894
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Very simple parse setup:

Code:
//send string using serial monitor
String readString, data1, data2, data3;

void setup() {
  Serial.begin(9600);
  Serial.println("parse-test-3"); // so I can keep track of what is loaded
}

void loop() {
  while (Serial.available()) {
    delay(2); 
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
    }
  }

  if (readString.length() >0) {
    Serial.println(readString); //see what was received

    // expect a string like 123456789 with three data partts     
    data1 = readString.substring(0, 3); //get the first three characters
    data2 = readString.substring(3, 6); //get the next three characters
    data3 = readString.substring(6, 9); //get the next three characters

    Serial.println(data1);  //print to serial monitor to see results
    Serial.println(data2);
    Serial.println(data3);

    readString="";
    data1="";
    data2="";
    data3="";
  }
}

Logged

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

Pages: [1]   Go Up
Jump to: