I need help with break one char into 3 others chars.

Hello everybody,

I want to break one char into another 3 chars.

i did this code:

Variables:

char teste[40] = "#TEMPO JAN 01 2001 01:01:01";
int cont;
char tmp[5] = "0";
char data[10] = "0";
char hora[7] = "0";

loop:

for(cont = 0; cont < 6; cont++){
    tmp[cont] = teste[cont];
  }
  Serial.println(tmp);
  for(cont = 0; cont < 11; cont++){
    data[cont] = teste[cont+7];
  }    
  Serial.println(data);
  for(cont = 0; cont < 8; cont++){
    hora[cont] = teste[cont+19];
  }
  Serial.println(hora);

and get my expected result on serial:

#TEMPO
JAN 01 2001
01:01:01

But if i change the serial prints to the end of all “for’s”, like this:

for(cont = 0; cont < 6; cont++){
    tmp[cont] = teste[cont];
  }
  
  for(cont = 0; cont < 11; cont++){
    data[cont] = teste[cont+7];
  }    
  
  for(cont = 0; cont < 8; cont++){
    hora[cont] = teste[cont+19];
  }
  Serial.println(tmp);
  Serial.println(data);
  Serial.println(hora);

i have this result:

#TEMPOJAN 01 20001:01:01
JAN 01 20001:01:01
01:01:01

i need the first result after all loops, someone can help me?

Sorry for my bad english.

i need the first result after all loops, someone can help me?

A string is a NULL terminated array of chars, NOT just an array of chars. Your for loops are not NULL terminating the array, so when you print the array, printing happens until a NULL is found, belonging to some other variable.

for(cont = 0; cont < 6; cont++){
    tmp[cont] = teste[cont];
    tmp[cont+1] = '\0'; // NULL terminate the array.
  }

Your loop tests are wrong, off by one. Should be cont < 5, cont < 10 and cont < 7 respectively. Or alternatively you array sizes are wrong.

#TEMPO” is 6 chars long, so you need a 7 char array.
“JAN 01 2001” is eleven chars long, so you need a 12 char array.
“01:01:01” is 8 chars long so you need a 9 char array.

I want to break one char into another 3 chars.

Very simple code that might do what you want.

//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 #TEMPO JAN 01 2001 01:01:01 with three data partts      
    data1 = readString.substring(0, 6); //get the first three characters
    data2 = readString.substring(7, 18); //get the next three characters 
    data3 = readString.substring(19, 28); //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="";
  } 
}

zoomkat:

I want to break one char into another 3 chars.

Very simple code that might do what you want.

//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 #TEMPO JAN 01 2001 01:01:01 with three data partts     
    data1 = readString.substring(0, 6); //get the first three characters
    data2 = readString.substring(7, 18); //get the next three characters
    data3 = readString.substring(19, 28); //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="";
  }
}

Thanks all for help, i fixed it.