Pages: [1]   Go Down
Author Topic: String concatenating  (Read 9251 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 32
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
char *msg = "00";

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

Code:
*msg = "004";

What should I do?
I've tried these:
Code:
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... smiley-wink
Logged

Visit my website: http://ricardo-dias.com/ for Arduinos and other stuff! smiley

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 375
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Check out the TextString library.

http://www.arduino.cc/en/Tutorial/TextString
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 32
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello!

As I am using VirtualWire, I had to #undef some "vars", such as:
Code:
#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?
Logged

Visit my website: http://ricardo-dias.com/ for Arduinos and other stuff! smiley

Connecticut, US
Offline Offline
Edison Member
*
Karma: 2
Posts: 1036
Whatduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
« Last Edit: February 14, 2009, 03:30:27 pm by halley » Logged

Massachusetts
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
N1VAN
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
« Last Edit: January 06, 2011, 01:32:17 pm by SSzretter » Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 601
Posts: 48554
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
This does not work
Does not compile, or does not produce correct results?

Which version of Arduino?
Logged

Massachusetts
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
N1VAN
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 601
Posts: 48554
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Massachusetts
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
N1VAN
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Looks like 0021
Logged

Pages: [1]   Go Up
Jump to: