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


hello everybody,

i want break 1 char 3 chars.

i did code:

variables:
code: [select]

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:
code: [select]

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 expected result on serial:
code: [select]

#tempo
jan 01 2001
01:01:01

but if change serial prints end of "for's", this:
code: [select]

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 result:
code: [select]

#tempojan 01 20001:01:01
jan 01 20001:01:01
01:01:01

i need first result after loops, can me?

sorry bad english.

quote
i need first result after loops, can me?

a string null terminated array of chars, not array of chars. loops not null terminating array, when print array, printing happens until null found, belonging other variable.

code: [select]
for(cont = 0; cont < 6; cont++){
    tmp[cont] = teste[cont];
    tmp[cont+1] = '\0'; // null terminate array.
  }


Arduino Forum > Using Arduino > Programming Questions > I need help with break one char into 3 others chars.


arduino

Comments

Popular posts from this blog

opencv3, tbb and rasp pi 2 - Raspberry Pi Forums

small ethernet problem - Raspberry Pi Forums

Multithumb configuration params not working? - Joomla! Forum - community, help and support