integer array to string

Status
Not open for further replies.

muggins

Well-known member
Hi
I have read that intensive string concantenation is memory hungry and I could use a little help.
I have a very large integer array of analogue readings and I need to parcel them up to send over a socket connection.
Is there a better way?

int JMarray[900];
..... ( populate array) ....
String timestr=String(now());
int arrayValue;
String str="";
String FullString;
FullString+= timestr;
FullString+= ",";

for(int i=0; i<=900; i++) {
arrayValue=JMarray;
str = String(arrayValue);

FullString+=str;
FullString+=",";
}
.........
server.println(FullString);
 
Last edited:
First, is the code working with the String class? If so, why change? If not, what makes you think the String class is causing the trouble? Please give us a complete example with your description of what goes wrong and what you've already tried to fix it. Please use the "Go Advanced" button and use the # button to wrap code tags around the code.

If you really are running to memory issues with the String class, simply don't use the String class. Use Serial.print() to perform the conversions from int to ASCII bytes (characters).
 
Hi
Thanks for coming back. Actually I'm not having problems with this. I was just wondering if there was a better way to approach this.
 
Two things come to mind: recently someone posted a way to measure memory usage at runtime. And the source for the String class is available. You could use both do get a better idea of what's going on under the covers. Next you might look at the resource utilization of various printf implementations on the Teensy. Finally you might look at more efficient wire protocols, like Google's Protocol Buffers. They have more compact wire representations of numbers. Enjoy!
 
The String class as a reserve() function. You can use that to pre-allocate enough memory for the largest string you'll make. That avoids the problems of constantly growing the string (which may or may not actually be an issue, depending on other code you write).
 
I have declared an integer array and a string
String FullString;
int JMarray[1200];

1200 values is the maximum I need.
This gets populated regularly with values and then I do an iteration through the array, each one concantenating a string, and then sent out to a socket connection.
This is very messy but it works and memory usage seems OK.
 
if your data values permit, consider if you can make the array int16_t a.k.a. short, to save memory.
 
Actually the question, I would as, is what are you doing with this data at the other side?

Do you really need it as a string, or could you not simply pass the binary data to the other side?
i.e. - Could you generate a simple packet: maybe something like: <count> <low byte data 0> <high byte data 0><low byte Data 1> <high byte Data 1> ...
Obviously if your data won't fit in a word, than the low/high, could be replaced by <byte 0> <byte 1>...
If you go this route, you may not be able to assume that the byte order for integers is the same for the two different sides.
 
Status
Not open for further replies.
Back
Top