Pages: [1]   Go Down
Author Topic: Concatenation  (Read 331 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello, I pretty new in arduino stuff and I am working on how to concatenate two const char* and how to convert from int to const char*.

I appreciate your help.

Thanks...
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 654
Posts: 50931
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I am working on how to concatenate two const char*
What have you tried? The strcat() function, maybe?

Quote
and how to convert from int to const char*.
atoi()
Logged

UK
Offline Offline
Faraday Member
**
Karma: 100
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Firstly, you can't convert anything to "const" char - the simple fact that it's const means it can't be changed, and so can't have anything converted into it smiley-razz

Concatenation can be done in a huge number of ways.  Char * is just a block of memory - you just need to copy that memory around the place.

For example, you could use snprintf:

Code:
char *s1 = "String 1";
char *s2 = "String 2";

char res[20]; // ensure there is enough room for both strings.

snprintf(res, 20, "%s %s", s1, s2);
// res contains "String 1 String 2"

This is the safest, but most heavy-weight way of doing it.  This ensures that the terminating NULL is always in place, and also that the string will never overflow (snprintf as opposed to sprintf).

Faster would be a memory copy:

Code:
char *s1 = "String 1";
char *s2 = "String 2";

char res[20]; // ensure there is enough room for both strings.

memcpy(res, s1, 8);
res[8] = ' ';
memcpy(res+9, s2, 8);
res[17] = 0;
// res contains "String 1 String 2"

Note the manual adding of the space between the strings, and the adding of the NULL at the end of the string.  

You could use strcpy as well, which would take the NULL into account:

Code:
char *s1 = "String 1";
char *s2 = "String 2";

char res[20]; // ensure there is enough room for both strings.

strcpy(res, s1, 8);
res[8] = ' ';
strcpy(res+9, s2, 8);
// res contains "String 1 String 2"

Now, for converting integers into char * you can use the sprintf (or snprintf for safety) function:

Code:
char out[10];
int val = 4729;

snprintf(out, 10, "%d", val);
// out contains "4729".

You can do a lot with sprintf / snprintf, but it can be quite resource / memory hungry.

« Last Edit: January 17, 2013, 03:13:43 pm by majenko » Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26633
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

strcat takes a constant string pointer, and appends it it to the end of a non-constant string pointer.
A couple of calls should get you near where you want to be.

For the other part, try "itoa"

Or roll the whole lot into one with sprintf
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

UK
Offline Offline
Faraday Member
**
Karma: 100
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
and how to convert from int to const char*.
atoi()

... oops ... ?

A ... to ... I ?   INT to CHAR *?
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 654
Posts: 50931
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
This is the safest
How? That won't even compile. sprintf and snprintf are different functions.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 654
Posts: 50931
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
... oops ... ?
I knew what I meant.  smiley-cool

I meant itoa().
Logged

UK
Offline Offline
Faraday Member
**
Karma: 100
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
This is the safest
How? That won't even compile. sprintf and snprintf are different functions.
It should have read snprintf in the code.  well spotted, changed.

And safest because snprintf will never write past the end of the string (as long as you set the limiting number right).
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

UK
Offline Offline
Faraday Member
**
Karma: 100
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
... oops ... ?
I knew what I meant.  smiley-cool

I meant itoa().
I don't like itoa, it's non-standard.  It's a bad habit to get into if you're ever thinking of programming on a system that isn't the arduino, or happens to use g++...
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Pages: [1]   Go Up
Jump to: