Go Down

Topic: String concatenation and printing (Read 17729 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.
The art of getting good answers lies in asking good questions.

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.
The art of getting good answers lies in asking good questions.

GoForSmoke

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

1) http://gammon.com.au/blink  <-- tasking Arduino 1-2-3
2) http://gammon.com.au/serial <-- techniques howto
3) http://gammon.com.au/interrupts
Tasking: techniques make the trees, the sketch is the woods.
See the woods and the trees, comment both.

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)

sb44

Hi samtal - not sure why such critical-toned responses to your post.  It may be good to hear there are better ways for this environment, but your question at least should be considered.

I'm new to Wiring language and confused by these examples offered in Arduino documentation:

Quote
Caution: You should be careful about concatenating multiple variable types on the same line, as you may get unexpected results. For example:

  int sensorValue = analogRead(A0);
  String stringOne = "Sensor value: ";
  String stringThree = stringOne + sensorValue;
  Serial.println(stringThree); 

results in "Sensor Value: 402" or whatever the analogRead() result is, but

  int sensorValue = analogRead(A0);
  String stringThree = "Sensor value: " + sensorValue;
  Serial.println(stringThree); 

gives unpredictable results because stringThree never got an initial value before you started concatenating different data types.
https://www.arduino.cc/en/Tutorial/StringAdditionOperator

Both code snippets would work fine in Java so I'm wondering whatthe Wiring compiler does differently, or if the second example is a mistake.

(Point taken that the String class and concatenating strings in this way carries overhead that may be undesirable on limited resource machines).




GoForSmoke

not sure why such critical-toned responses to your post. 
We discourage bad practices. It helps to cut down on Arduino beginners developing BAD HABITS that end up with them trying to figure out why their sketch crashed.
 
String class is okay on my computer so it's okay on Arduino is about puddle deep. That's like saying my house has a bathtub so my bicycle can have one too. Even empty it's a kludge. Let's not teach beginners to put bathroom fixtures on bicycles even if we can.

1) http://gammon.com.au/blink  <-- tasking Arduino 1-2-3
2) http://gammon.com.au/serial <-- techniques howto
3) http://gammon.com.au/interrupts
Tasking: techniques make the trees, the sketch is the woods.
See the woods and the trees, comment both.

sb44

Sure, I know I can use all the guidance I can get.  I just don't see what mean-spirited tone accomplishes.

GoForSmoke

You see how you interpret.

BTW, this is a tech forum not a modern university "safe space" with cuddle toys and blankets.
1) http://gammon.com.au/blink  <-- tasking Arduino 1-2-3
2) http://gammon.com.au/serial <-- techniques howto
3) http://gammon.com.au/interrupts
Tasking: techniques make the trees, the sketch is the woods.
See the woods and the trees, comment both.

noweare

Oh, wow, yeah mean spirited is the way I read it too.
You don't know these guys background so why be so heavy handed.
I don't think you helped much.
The guy might never want to post again.
If  you want to play with the big boys go over to the stm32 forum and hang out there.

GoForSmoke

Sure, I know I can use all the guidance I can get.  I just don't see what mean-spirited tone accomplishes.
I don't see what "I'm sticking to this way even if you all tell me it's a mistake" gets you.

Members took time to tell you better and you blew them off then play victim when they try to wise you up.

But hey, you waste all of your own time you want, I bet you don't get upset over that.
1) http://gammon.com.au/blink  <-- tasking Arduino 1-2-3
2) http://gammon.com.au/serial <-- techniques howto
3) http://gammon.com.au/interrupts
Tasking: techniques make the trees, the sketch is the woods.
See the woods and the trees, comment both.

causalloop

I don't see what "I'm sticking to this way even if you all tell me it's a mistake" gets you.

Members took time to tell you better and you blew them off then play victim when they try to wise you up.

But hey, you waste all of your own time you want, I bet you don't get upset over that.
I'm a few years late (and a dollar short) - and not to beat a dead horse here - but I came across this thread looking for - what I thought - would be the right solution (I was wrong) to something string related, and while admittedly I don't have all the context, I've gotten the *ss end of the stick enough that I felt the need (desire?) to chime in..

I don't know what the right term is, trolling, mean-spirited, heavy handed, just being a jerk, take your pick.. What I get out of the above discussion is that the OP went looking for a solution and instead of getting helpful guidance, he got beaten with a stick and told to RTFM.  Just to be clear, this is a 'community' forum, not a lecture hall.  'Quit being lazy'?  Really?  That was the nice - or more to the point - helpful way of wording that?

Quote
You see how you interpret.

BTW, this is a tech forum not a modern university "safe space" with cuddle toys and blankets.
Wow.  Just.. wow.  I see how he interprets BTW.  And this is a Tech Forum, AND it's supposed to be a safe space.  If you don't want to answer questions for "n00b", then don't answer.  If you can't take the time to meet people where they're at, why bother replying at all?  noweare is right - he may have never posted again.  If I knew that these are the kind of replies I'd get, I sure as hell wouldn't.  To be blunt, you're not wrong, you're just *ssholes.  If people treated you guys like this when you were learning, can you honestly say you'd be receptive?  Having dealt with this crap in the past, just because I hate it when people do this kind of thing, I would have dragged this out as long as humanly possible JUST to waste your time. 

When you reply to people asking for help, if you're too superior to not act like a decent person, don't go kicking them in the crotch, just don't say anything - move along.  You want to discourage bad habbits?  Fine, I get that - I even agree with it - but there's discouraging by teaching, and then there's discouraging by shooting people in the knee caps.  Which do you think is a more useful teaching tool?  One way, you'll get someone grateful, who'll learn and listen and respect - and be a better programmer for it.  The other way, you'll end up with someone so  turned off by all this that they give up entirely, not because they're idiots or incapable, but because all you taught them as that the "community" of peers isn't really willing to help; all they're interested in is having pissing contests and showing off how much more they know than you do. 

I'm not a programmer, I don't exercise best practices - not because I'm lazy, but there's a difference between not asking questions, and not knowing that you needed to ask questions.  There's an opportunity here to help someone who might have ideas you never even considered - even if they're n00bs now.  You don't think the Einsteins, Teslas (the guy and the car.. guy), and anyone else who became amazing at what they do (maybe Musk wasn't a great example), didn't ask some pretty stupid sounding questions at some point?  Imagine where we'd be if every time Edison got the light bulb wrong, some guy came in with a golf club an broke a leg?  Think he'd have invented the light bulb? (lets just use him as the inventor metaphorically since its arguable if the man himself was the "inventor") - I think Edison would have said screw it, and gone back to candles. 

Now that I've wasted 45 minutes on this, back to work (and to await someone's heavy handed response about how I'm just an overly sensitive baby who doesn't belong in the tech world or on the interwebs).  Seriously though, "you're wrong and you're an idiot for being wrong" is not an effective teaching tool.  No, thats not a direct quote obviously, but thats most definitely what was effectively said.  THIS is why the world is all about competition and not collaboration.  Everyone's got this need to prove how big their junk is.. what a waste of EVERYONE's time (now including my own..)  Civil.  Thats the word I've been looking for.  Civil.  Be civil.  Be kind, be humble, don't be cruel, don't be judgmental or presumptuous; these things lead to hate and only bread more hate.  Call me hippy dippy all you want, but this is as bad as cops abusing authority because they can.  Shame on you - and you'll know who you are because you're most likely to be the first ones to reply to this with more pettiness.  Feel free to kick me off my soap box and put me in whatever place you think I belong; you'll only be proving my point.  (This is my only reply to this btw, yeah, I have a pretty good idea what the response to that is going to be - something along the lines of walking away from a fight, or being too pansy or chicken to stick around, or maybe *I'm* the petty one.  Whatever the case may be, I've said my piece.)  Hopefully this gets taken as a helpful calling out and not a way of picking a fight.  I just don't like seeing the little people getting pissed on.

Go Up