subtracting character from string or char array

I have a array it looks like

1,2,3,4,

I need to remove the comma from the end of the last entry and add a bracket to the beginning and end like,

[1,2,3,4]

Here is how the server send my string response,

  String temp_string = _temp1Struct.array ;
  server.send(200, "text/plane", "[" + temp_string + "]");

the above sends the response like this,

[1,2,3,4,]

i need [1,2,3,4]

What can i do?

OR even better, here is the code i use to make the array,

void handle_debug_messages(char message[]) {
  if (strlen(_temp1Struct.array) + strlen(message) + 2 >= 1500) {
    _temp1Struct.array [0] = '\0';
  } else {
    strcat(_temp1Struct.array, message);
    strcat(_temp1Struct.array, ", ");
  }
}

notice i append a , at then end of every entry. how could i prevent from adding the comma to the last entry?

I tried

void handle_temp1_console() {
  byte lastChar = strlen(_temp1Struct.array) - 1;
  _temp1Struct.array[lastChar] = '\0';
  String temp_string = _temp1Struct.array ;
  server.send(200, "text/plane", "[" + temp_string + "]");
}

but its sending the data with the last entry cut off like this,

[81.32, 81.32, 81.32, 81.32, 81.32, 81.32, 81.32, 81.32, 81.32, 81.32, 8]

try

int main(void) {
    char data[50]="1,2,3,4,";
        printf("%s\n", data);
    data[strlen(data)-1]=']';
    printf("%s\n", data);
    memmove(&data[1], data, strlen(data));
    data[0]='[';
    printf("%s\n", data);
    }

gives

1,2,3,4,
1,2,3,4]
[1,2,3,4]

see memmove()

horace:
try

int main(void) {

char data[50]="1,2,3,4,";
       printf("%s\n", data);
   data[strlen(data)-1]=']';
   printf("%s\n", data);
   memmove(&data[1], data, strlen(data));
   data[0]='[';
   printf("%s\n", data);
   }



gives


1,2,3,4,
1,2,3,4]
[1,2,3,4]



see [memmove()](http://www.cplusplus.com/reference/cstring/memmove/)

will this cause problems with my format if my array entries have deciamls. i should have showed how the data really looks,

I tried something similar i think.. and this is what the data looked like. sometimes its right sometimes the numbers are cutoff.

[81.14,81.14,81.14,81.14,81.14,81.14,81.14,81.14,81.14,81.68,81.50,81.50,81.32,81.32,81.14,81.50,92.84,97.52,97.88,96.98,95.72,94.46,93.38,92.30,9]

I do see memmove() but i dont know what its doing. the way i see it my original code does the same thing, replaces the last character with null terminator

void handle_temp1_console() {
  byte lastChar = strlen(_temp1Struct.array) - 1;
  _temp1Struct.array[lastChar] = '\0';
  String temp_string = _temp1Struct.array ;
  server.send(200, "text/plane", "[" + temp_string + "]");
}

so long as the string ends with , and you require [ at the front and ] at the end decimals should make no difference, e..g.

int main(void) {
    char data[200]="81.14,81.14,81.14,81.14,81.14,81.14,81.14,81.14,81.14,81.68,81.50,81.50,81.32,81.32,81.14,81.50,92.84,97.52,97.88,96.98,95.72,94.46,93.38,92.30,99,";
        printf("%s\n", data);
    data[strlen(data)-1]=']';
    printf("%s\n", data);
    memmove(&data[1], data, strlen(data));
    data[0]='[';
    printf("%s\n", data);
    }

gives

81.14,81.14,81.14,81.14,81.14,81.14,81.14,81.14,81.14,81.68,81.50,81.50,81.32,81.32,81.14,81.50,92.84,97.52,97.88,96.98,95.72,94.46,93.38,92.30,99,
81.14,81.14,81.14,81.14,81.14,81.14,81.14,81.14,81.14,81.68,81.50,81.50,81.32,81.32,81.14,81.50,92.84,97.52,97.88,96.98,95.72,94.46,93.38,92.30,99]
[81.14,81.14,81.14,81.14,81.14,81.14,81.14,81.14,81.14,81.68,81.50,81.50,81.32,81.32,81.14,81.50,92.84,97.52,97.88,96.98,95.72,94.46,93.38,92.30,99]

or is there something else going on?

memmove() moves the array elements one element up, e..g. data[0] to data[1], data[1] to data[2], etc enabling [ to be inserted in element data[0]

your code to replace the , with '\0' looks OK
try some print() statements to see exactly what is going on

I might be doing something wrong trying to adapt to your example my data looks like this,

[[[[81.14,81.14,81.14,81.14,81.14,81.14,81.14,81.14,81.14,81.68,81.50,81.50,81.32,81.32,81.14,81.50,92.84,97.52,97.88,96.98,95.72,94.46,93.38,92.30,91.40,90.50,89.78,89.06,88.52,87.98,87.44,87.08,86.54,86.00,85.64,85.28,85.10,84.74,84.56,84.20,84.02,83.84,83.48,83.30,83.12,82.94,82.94,82.76,82.58,82.58,82.22,82.22,82.04,81.86,81.68,81.68,81.68,81.50,81.50,81.32,81.32,81.32,81.14,81.14,80.96,80.96,80.96,80.78,80.78,80.78,80.60,80.60,80.60,80.60,80.60,80.60,80.42,80.42,80.42,80.42,80.24,80.24,80.24,80.06,80.06,80.06,80.06,80.06,80.06,80.06,80.06,80.06,79.88,79.88,79.88,79.88,79.88,79.88,79.88,79.88,79.88,79.88,79.88,79.88,79.88,79.88,79.88,79.88,79.88,79.88,79.88,79.88,79.70,79.70,79.70,79.88,79.70,79.70,79.70,79.70,79.70,79.70,79.70,79.70,79.70,79.70,79.70,79.70,79.70,79.70,79.70,79.70,79.70,79.70,79.70,79.70,79.70,79.52,79.52,79.52,79.52,79.52,79.70,79.52,79.70,79.70,79.70,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52]
  _temp1Struct.array[strlen(_temp1Struct.array) - 1] = ']';
  memmove(&_temp1Struct.array[1], _temp1Struct.array, strlen(_temp1Struct.array));
  _temp1Struct.array[0] = '[';
 
  String temp_string = _temp1Struct.array ;
  server.send(200, "text/plane", temp_string);
}

Unless im crazy. it looks like i only see the extra bracket at the beginning if i refresh the browser fast

it looks like you have done the memmove() etc several times?
it should only be done once
how do you initialise the _temp1Struct.array?

horace:
it looks like you have done the memmove() etc several times?
it should only be done once
how do you initialise the _temp1Struct.array?

initially the struct that contains array[] is initialized on another node where the data is copied into it and sent to another node,

struct {
  int Humidity;
  float temp;
  char array[1500];
} _temp1Struct;


  if (millis() - now2 >= 8000) {
    String temp =  String(_temp1Struct.temp );
    char _temp[7];
    temp.toCharArray(_temp, 7);
    handle_debug_messages(_temp);
    now2 = millis();
  }


void handle_debug_messages(char message[]) {
  if (strlen(_temp1Struct.array) + strlen(message) + 2 >= 1500) {
    _temp1Struct.array [0] = '\0';
  } else {
    strcat(_temp1Struct.array, message);
    strcat(_temp1Struct.array, ",");
  }
}

void sendStruct(IPAddress to, char delimiter[], char delimiter2[]) {
  Udp.beginPacket(to, ServerPort);
  Udp.write(delimiter);
  Udp.write((uint8_t*)&_temp1Struct, sizeof(_temp1Struct)); //cast to bytes
  Udp.write(delimiter2);
  Udp.endPacket();
}

then on another node i send it to the server like this,

void handle_temp1_console() {
  _temp1Struct.array[strlen(_temp1Struct.array) - 1] = ']';
  memmove(&_temp1Struct.array[1], _temp1Struct.array, strlen(_temp1Struct.array));
  _temp1Struct.array[0] = '[';
 
  String temp_string = _temp1Struct.array ;
  server.send(200, "text/plane", temp_string);
}

please forgive the naming scheme

also sometimes the data still looks like this,

[79.34,79.52,79.34,79.34,79.52,79.34,79.34,79.34,79.34,79.34,79.34,79.34,79.34,79.34,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52]2.94,82.76,82.58,82.58,82.22,82.22,82.04,81.86,81.68,81.68,81.68,81.50,81.50,81.32,81.32,81.32,81.14,81.14,80.96,80.96,80.96,80.78,80.78,80.78,80.60,80.60,80.60,80.60,80.60,80.60,80.42,80.42,80.42,80.42,80.24,80.24,80.24,80.06,80.06,80.06,80.06,80.06,80.06,80.06,80.06,80.06,79.88,79.88,79.88,79.88,79.88,79.88,79.88,79.88,79.88,79.88,79.88,79.88,79.88,79.88,79.88,79.88,79.88,79.88,79.88,79.88,79.70,79.70,79.70,79.88,79.70,79.70,79.70,79.70,79.70,79.70,79.70,79.70,79.70,79.70,79.70,79.70,79.70,79.70,79.70,79.70,79.70,79.70,79.70,79.70,79.70,79.52,79.52,79.52,79.52,79.52,79.70,79.52,79.70,79.70,79.70,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.52,79.34,79.52,79.34,79.52,79.34,79.34,79.34,79.34,79.34,79.34,79.34,79.34,79.52,79.52,79.52,79.52,79.34,79.34,79.34,79.34,79.34,79.34,79.52,79.34,79.34,79.52,79.52,79.52,79.52,79.34,79.52,79.52,79.52,79.34,79.34,79.34,79.34,79.52,79.34,79.34,79.34,79.52,79.34,79.34,79.34,79.34,79.34,79.34,

my guess is maybe array is being overwritten before the code has time to execute?

is this before of after the code to remove the , and add [ ... ]
if you have multiple nodes sending data to each other could you be receiving a new array before you have finished processing the last one?
I think we need to know more about the sequence of events

use double buffering?

horace:
is this before of after the code to remove the , and add [ ... ]
if you have multiple nodes sending data to each other could you be receiving a new array before you have finished processing the last one?
I think we need to know more about the sequence of events

use double buffering?

im running an esp8266 and im getting scarce on ram. on the first node where i add data to array[] i tried memmove() and adding the braces there but the result is unexpected to me,

[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]79.70]79.70]79.70]79.70]79.70]79.70]79.70]79.70]79.70]79.70]79.70]79.70]79.70]79.70]79.70]
void handle_debug_messages(char message[]) {
  if (strlen(_temp1Struct.array) + strlen(message) + 2 >= 1500) {
    _temp1Struct.array [0] = '\0';
  } else {
    strcat(_temp1Struct.array, message);
    strcat(_temp1Struct.array, ",");
    _temp1Struct.array[strlen(_temp1Struct.array) - 1] = ']';
    memmove(&_temp1Struct.array[1], _temp1Struct.array, strlen(_temp1Struct.array));
    _temp1Struct.array[0] = '[';
  }
}

im trying to think. does it make more sense to use the memmove() and add the braces to the code that write to array[]? instead of writing to it again after receiving it? the tempSensor node that writes to the array has plenty of ram left

it looks like every time you add message[] to _temp1Struct.array[] you are adding [ and ]
you only need to that at the end when you have the complete array

horace:
it looks like every time you add message[] to _temp1Struct.array[] you are adding [ and ]
you only need to that at the end when you have the complete array

well array[] is being written to and cleared all the time so i cant wait until its full to send it.

What i did is in the first code that populates array[].

void handle_debug_messages(char message[]) {
  if (strlen(_temp1Struct.array) + strlen(message) + 2 >= 1500) {
    _temp1Struct.array [0] = '\0';
  } else {
    _temp1Struct.array [0] = '[';
    strcat(_temp1Struct.array, message);
    strcat(_temp1Struct.array, ",");
  }
}

this adds the first brace every time.

Then on the second node that sends array[] to the server as a String i did this,

void handle_temp1_console() {
  _temp1Struct.array[strlen(_temp1Struct.array) - 1] = ' ';
  String temp_string = _temp1Struct.array ;
  server.send(200, "text/plane", temp_string + "]");
}

i think really i scould just send array[] from the server like this,

server.send(200, "text/plane","[" + temp_string + "]");

and that would work too. fingers crossed it appears to be working fine now

notsolowki:
well array[] is being written to and cleared all the time so i cant wait until its full to send it.

sounds like you could get data corruptions if you are transmitting an array at the same time as filling it

consider double buffering where you have two arrays

  1. fill array 1 - when full start transmitting it
  2. start filling array 2 - when full start transmitting it
    hopefully when array 2 is full array 1 should have finished transmitting so you can start filling it

if you receive data faster than you can transmit it you will get data overruns

if speed of data reception/transmission varies consider using a ring buffer