Go Down

Topic: String concatenating (Read 10224 times) previous topic - next topic

RicDias

Hi all.

I'm trying to find a way to declare a string variable in order to send it with Virtual Wire.

Imagine I have this initial string:

Code: [Select]
char *msg = "00";

Now I want to add one "4" to the end, so I have the new string:

Code: [Select]
*msg = "004";

What should I do?
I've tried these:
Code: [Select]
char *msg += "4";
char *msg &&= "4";
char *msg &= "4";
char *msg .= "4";
char *msg = *msg + "4";
char *msg = *msg && "4";
char *msg =*msg & "4";
char *msg = *msg . "4";


None of these worked... I think you got the idea and hope you can help me... ;)
Visit my website: http://ricardo-dias.com/ for Arduinos and other stuff! :)


RicDias

Hello!

As I am using VirtualWire, I had to #undef some "vars", such as:
Code: [Select]
#undef int
#undef abs
#undef double
#undef float
#undef round


Now it seems that TextString lib requires them.

Is there a way to get around this issue?
Visit my website: http://ricardo-dias.com/ for Arduinos and other stuff! :)

halley

#3
Feb 14, 2009, 09:28 pm Last Edit: Feb 14, 2009, 09:30 pm by halley Reason: 1
Code: [Select]
char buffer[10];     // needs to be big enough to hold the longest string plus a terminator
// ...
strcpy(buffer, "00");  // puts digit '0' digit '0' terminator '\0' into buffer
strcat(buffer, "4");   // looks for terminator, copies digit '4' terminator '\0' there


Do some google searching for tutorials using strcat() and strcpy() to start to get an understanding of strings in C.  There are quite a few ways to get strings wrong, which can cause your program to crash or act badly.

SSzretter

#4
Jan 06, 2011, 03:18 pm Last Edit: Jan 06, 2011, 07:32 pm by SSzretter Reason: 1
I am trying to figure out a concat issue - I want to take 3 shorts and make a semicolon delimited string:

This does not work:
 tempStr = String(outlet1AStatus);
    tempStr = tempStr + ";";
    tempStr = tempStr +String(outlet1ACycles);
    tempStr = tempStr + ";";
    tempStr = tempStr +String(outlet1BStatus);
    tempStr = tempStr + ";";
    tempStr = tempStr +String(outlet1BCycles);
   
 (I have tried several variations of this, and I end up with either nothing, a control character (box), or a 0 )


This alone works:
 tempStr = String(outlet1AStatus);  // 0
    tempStr = tempStr + ";";

 (end up with a string of  "0;")

This also works:
tempStr = String(outlet1AStatus);  //0
    tempStr = tempStr +String(outlet1ACycles); //1
    tempStr = tempStr +String(outlet1BStatus); //0
    tempStr = tempStr +String(outlet1BCycles); //1

(for example it would end up a string of "0101")

So how do I get my semicolons and values?

PaulS

Quote
This does not work

Does not compile, or does not produce correct results?

Which version of Arduino?

SSzretter

I updated my post to explain a little more.

The other thing that is happening , I think the Arduino is crashing in the first example - but only after quite a few iterations - for example, maybe 1000 times or more.     As soon as I change it to the version without the semicolons, no problem, no hang.


PaulS

Which version of the Arduino IDE are you using? The String class has changed, over time.

SSzretter


Go Up