dtostrf -- doing my head in, or rather coming back with garbage

I have an array, which I split into 12 tokens within my void loop()

strRecordIn = 56.0,0.5,-0,100,830,960,0,400,4100,10240,4095,20

  char strRecordIn[RECORD_IN_ARRAY_SIZE];   // one record in from BMS; every second a new one
  char strRecordOut[RECORD_OUT_ARRAY_SIZE]; // one record out from BMS

If a run a for() through strRecordIn , I see all tokens

I am add some of the values to strRecordOut, like so:

      // create array of BMS values
      int arrayElements = 11;               // number of BMS values (12)
      char *arrValues[arrayElements];       // array holding values
      
      arrValues[0] = strtok(strRecordIn, ","); 
      
      for (int j = 1; j <=arrayElements; j++) {
        arrValues[j] = strtok(NULL, ",");    // Split string into tokens
      }
      
      strcat(strRecordOut, clock.dateFormat("d/m/Y H:i:s", dt));        // add delimiter
      
      for (int i = 0; i <= 3; i++) {         // VLT, AMP, AHR, SOC
        strcat(strRecordOut, ",");           // add delimiter
        strcat(strRecordOut, arrValues[i]);  // add value
      }

      if (intLoopCounter == 50) {            // every 50 records read temperature sensor
        sensors.requestTemperatures();       // get temperatues
        currentTemp = sensors.getTempCByIndex(0); // read 1st temperature sensor
        intLoopCounter = 0;                  // reset 
      }

      // here: temp float value, -12.34 (6), 2, buf = 6+\0
      //char charBuf[] = "";
      //dtostrf(currentTemp, 6, 2, charBuf); // convert float to char
      strcat(strRecordOut, ",");             // add delimiter
      //strcat(strRecordOut, charBuf);       // ad temperature
      strcat(strRecordOut, "22.22");         // ad temperature
      
      // get field 7 -- ERR error value      // ERR
      strcat(strRecordOut, ",");             // add delimiter
      strcat(strRecordOut, arrValues[6]);    // add error value

      // get field 12 -- TIM value           // TIM= BMS record count (0-45)
      strcat(strRecordOut, ",");             // add delimiter
      strcat(strRecordOut, arrValues[11]);   // add received record count

      for (int k = 0; k <=arrayElements; k++) {
        Serial.print(k);
        Serial.print(" ");
        Serial.println(arrValues[k]);
      }
   
      Serial.println(strRecordOut);
      strRecordIn[0]    = '\0';         // empty array
      strRecordOut[0]   = '\0';         // empty array
      recordInByteCount = 0;            // reset byte counter

When I check the array values, they are all OK (values here do not match the array above; as I am showing output of real values)

0 53.0
1 -13.7
2 -86
3 79
4 835
5 960
6 0
7 400
8 3958
9 1975
10 4095
11 4
08/07/2016 19:56:47,53.0,-13.7,-86,79,22.22,0,4 // strRecordOut

Now the interesting part. when I change this section
//char charBuf[] = "";
//dtostrf(currentTemp, 6, 2, charBuf); // convert float to char
strcat(strRecordOut, ","); // add delimiter
//strcat(strRecordOut, charBuf); // ad temperature
strcat(strRecordOut, "22.22"); // ad temperature
to:
char charBuf[] = "";
dtostrf(currentTemp, 6, 2, charBuf); // convert float to char
strcat(strRecordOut, ","); // add delimiter
strcat(strRecordOut, charBuf); // ad temperature

which changes the temperature float value to char, I l no longer have access to array value 11, and the output is like so:

0 52.9
1 -35.3
2 -88
3 78
4 833
5 960
6 0
7 400
8 3742
9 12616
10 4095
11 ™ê

08/07/2016 20:02:48,52.9,-35.3,-88,78, 24.13,0,™ê

Why on earth does it kill that last value? Any help/hint appreciated.

Also, the temperature value 24.13 has a space in front of it. Is there an easy way to get rid of it?

for (int j = 1; j <=arrayElements

You only have eleven elements in your array

I don't know if it's the answer to the problem, because of the lack of compilable code which is against forum rules...

But I do spot
char charBuf[] = "";

Which will just give you a char names charBuf with size 1. Because "" has length 1.

AWOL:

for (int j = 1; j <=arrayElements

You only have eleven elements in your array

Spot on!
The value of another set of eyes... I was sitting over ti for 3 hours :o
Thank you very much!

Fixed the other error as well? Because now you have a memory overflow...

As a rule-of-thumb, I always pay particular attention to the presence of "<=" in for loops where arrays are concerned.

The empty string mentioned above is also worth a closer look.

septillion:
lack of compilable code which is against forum rules...

char charBuf[] = "";
Which will just give you a char names charBuf with size 1. Because "" has length 1.

I read "compatible" and was wondering??? However, compilable you said... which is difficult as the loop is receiving data on the serial port, hence, my description.

dtostrf 'blows' up the char array to 6 ([-]dd.dd = 6)

AWOL:
The empty string mentioned above is also worth a closer look.

See above... that's how I understand it works. Did a strlen(charBuf) before and after dtostrf()

septillion:
Fixed the other error as well? Because now you have a memory overflow...

See above as well... but I am not certiain.

The output is now correct:
0 52.9
1 -15.0
2 -101
3 75
4 834
5 960
6 0
7 400
8 3945
9 10846
10 4095
11 22
08/07/2016 20:28:54,52.9,-15.0,-101,75, 24.25,0,22

tostrf 'blows' up the char array to 6 ([-]dd.dd = 6)

Something is likely to blow up, but it isn't your array.

MaxG:
I read "compatible" and was wondering??? However, compilable you said... which is difficult as the loop is receiving data on the serial port, hence, my description.

A discribtion is good but also add the full code OR make a simplyfied exaple with the same error. For example, instead of working on receiving data use fixed data that represents it. A lot of times the error isn't in the snippet. Because for example the error is due to a variable declaration or an error in the receiving part. Debugging snippets is just hard.

MaxG:
dtostrf 'blows' up the char array to 6 ([-]dd.dd = 6)

Nope, it does not. All var lengths are set on declaration. A variable can't grow. You declared it with size 1 so it will always be size 1.

This time you're probably lucky and the memory of &charBuf+1 isn't used or not critical. But you do overflow :wink:

septillion:
a) A discribtion is good but also add the full code OR make a simplyfied exaple with the same error.

b) Nope, it does not. All var lengths are set on declaration. A variable can't grow. You declared it with size 1 so it will always be size 1.

c) This time you're probably lucky and the memory of &charBuf+1 isn't used or not critical. But you do overflow :wink:

Thanks again :slight_smile:
a) understood
b) you're right
c) most likely

changed is to

char charBuf[7];

MaxG:
changed is to

char charBuf[7];

That's better. Be aware that a char of 7 limits you to values of 999,99. Any higher will still overflow.