Go Down

Topic: Concatenation (Read 355 times) previous topic - next topic

george2k

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...

PaulS

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()

majenko

#2
Jan 17, 2013, 09:10 pm Last Edit: Jan 17, 2013, 09:13 pm by majenko Reason: 1
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 :P

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: [Select]

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: [Select]

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: [Select]

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: [Select]

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.


AWOL

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
"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.

majenko


Quote
and how to convert from int to const char*.

atoi()


... oops ... ?

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

PaulS

Quote
This is the safest

How? That won't even compile. sprintf and snprintf are different functions.

PaulS

Quote
... oops ... ?

I knew what I meant.  8)

I meant itoa().

majenko


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).

majenko


Quote
... oops ... ?

I knew what I meant.  8)

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++...

Go Up