Parsing Data

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,,

budiraharjo:
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?

@budiraharjo:

If the data "123456789" is a String object as defined here: String() - Arduino Reference 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: Calculator with serial communication problem. - #2 by mrburnette - Programming Questions - Arduino Forum

  • Ray

If the data is in a variable then maths can do it for you:

mydata = 123456789;
data1 = mydata/1000000;
data2 = (mydata % 1000000)/1000;
data3 = (mydata % 1000);

mydata is a long
data1, data2 and data3 are ints.

Very simple parse setup:

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