Go Down

Topic: String concatenation and printing (Read 221 times) previous topic - next topic

samtal

Hi,
I have 2 strings in a mixed struct.
The strings are defined in the struct as char string
  • , and given string values.

To print out, I concatenate several strings into one longer string, and print it out via serial print command.
So far, so good.
Problem is that while it printed correctly in previous versions of my code, it does not print in a new version, with very little change from previous.
Unfortunately, I could not figure out what made the change.

Here is my code snippet:

#include <string.h>
.
.

struct defineConfigs {   
  .
  char configVersion[4];
  char versionDate[11];   
. (more items in the struct)
.
.
}
configData = {
 .
  "B60",
  "22-05-15",
.(more items)
.
}

The print command in the setup():

Serial.println((String)"Rev " + configData.configVersion + " " + configData.versionDate);

While in the previous version it printed out the strings as they appear, namely "Rev B60 22-05-15"
in the new version it always prints out "Rev 32".

I send the same string to the lcd, and get the same outcome.

There seem to be no relevant difference between the versions, and yet it will always print "Rev 32"
(32 is the ascii code for space(?))

What i the correct way to define and concatenate the strings and make them always print correctly?

Thanks

PaulS

Code: [Select]
Serial.println((String)"Rev " + configData.configVersion + " " + configData.versionDate);
Quit being lazy. Use as many Serial.print() statements as needed to output each piece of data. Quit pissing away resources (ab)using the String class that way.

HazardsMind

There is also the function sprintf(). You should think about looking into it.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

samtal

Thanks for all suggestions.
Eventually I found out there was no problem with the printing code, but rather with the data in the structure.
The suggested format using String concatenation is just fine, and has nothing to do with laziness.
It is a useful tool that was added to Arduino, and is the best choice.
(Such tool is available in all higher languages I know)

I use it for the simple reason that I re-use the same text in several locations, thus building a single string and re-using it is the correct approach.

In my code I use it: (and it is just fine)
String message = (String)"Rev " + configData.configVersion + "," + configData.versionDate;
Serial.println(message);
lcd.print(message);

samtal

HazardsMind


Quote
It is a useful tool that was added to Arduino, and is the best choice.
No it is not. At least not on an Arduino which has very limited memory. C strings (char arrays) are the preferred choice. You might have to write more, but they save you SO much more memory than Strings.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

PaulS

Quote
It is a useful tool that was added to Arduino, and is the best choice.
Nonsense.

Quote
(Such tool is available in all higher languages I know)
Where such languages are used on hardware with 16 gigs of real memory and an operating system that makes almost unlimited amounts of virtual memory available, such tools are fine. On an Arduino, with no OS, and a very small amount of memory, such tools are for fools.

GoForSmoke

I don't think that the OP understands the nature of serial data, limited RAM or C++ container classes.

Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

robtillaart


Code: [Select]
Serial.println((String)"Rev " + configData.configVersion + " " + configData.versionDate);

can be better written as

Code: [Select]
Serial.print("Rev ");
Serial.print(configData.configVersion);
Serial.print(" ");
Serial.println(configData.versionDate);


to use minimal memory.

Optionally you wrap it in a separate function:
Code: [Select]
printVersionInfo()
{
  Serial.print("Rev ");
  Serial.print(configData.configVersion);
  Serial.print(" ");
  Serial.println(configData.versionDate);
}


Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up