string concatenation
-
Hi, I want to concatenate a variable of type 'unsigned long' to a variable of type 'char []'. How can this be done, since strcat() function takes 'char' arguments? Thanks. -Kranti
-
Hi, I want to concatenate a variable of type 'unsigned long' to a variable of type 'char []'. How can this be done, since strcat() function takes 'char' arguments? Thanks. -Kranti
This is how we do it:
#include <iostream>
#include <sstream>
int main( )
{
using namespace std;
long lng = 2000;
stringstream strs;
strs<<"hello: "<
Nibu thomas
Software Developer -
Hi, I want to concatenate a variable of type 'unsigned long' to a variable of type 'char []'. How can this be done, since strcat() function takes 'char' arguments? Thanks. -Kranti
-
Hi, I want to concatenate a variable of type 'unsigned long' to a variable of type 'char []'. How can this be done, since strcat() function takes 'char' arguments? Thanks. -Kranti
Assuming that you by
char[]
really meanstd::string
(because you're a conscious[1] developer that avoids bald arrays and pointers if possible, right? ;)), then do like this:std::string ToString(unsigned long value) // Put this function in your local vault.
{
char buffer[33]; // '33' comes from the _ultoa documentation.return std::string(\_ultoa(value, buffer, 10));
}
std::string concatenated(original.append(ToString(value)));
[1] Conscientious? -- The Blog: Bits and Pieces -- modified at 5:09 Monday 27th February, 2006
-
Assuming that you by
char[]
really meanstd::string
(because you're a conscious[1] developer that avoids bald arrays and pointers if possible, right? ;)), then do like this:std::string ToString(unsigned long value) // Put this function in your local vault.
{
char buffer[33]; // '33' comes from the _ultoa documentation.return std::string(\_ultoa(value, buffer, 10));
}
std::string concatenated(original.append(ToString(value)));
[1] Conscientious? -- The Blog: Bits and Pieces -- modified at 5:09 Monday 27th February, 2006
Johann Gerell wrote:
Assuming that you by char[] really mean std::string
You assume too much. That wasn't what he asked at all.
Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
-
Assuming that you by
char[]
really meanstd::string
(because you're a conscious[1] developer that avoids bald arrays and pointers if possible, right? ;)), then do like this:std::string ToString(unsigned long value) // Put this function in your local vault.
{
char buffer[33]; // '33' comes from the _ultoa documentation.return std::string(\_ultoa(value, buffer, 10));
}
std::string concatenated(original.append(ToString(value)));
[1] Conscientious? -- The Blog: Bits and Pieces -- modified at 5:09 Monday 27th February, 2006
Johann Gerell wrote:
because you're a conscious developer
Well, I certainly hope he's conscious when he codes. Quite a feat to code while unconscious (although looking at some code makes one wonder:~ ). Or did you mean conscientious?
You may be right I may be crazy But it just may be a lunatic you’re looking for -- Billy Joel -- Within you lies the power for good - Use it!
-
Johann Gerell wrote:
Assuming that you by char[] really mean std::string
You assume too much. That wasn't what he asked at all.
Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
Ryan Binns wrote:
You assume too much.
That assumption was more of a veiled desire. I could have replied: "If you're using
char[]
instead ofstd::string
, then I'll be more than willing to answer your question if you restate it using astd::string
instead." But that would've been a bit too pompous, wouldn't it? So, I stuck in my (maybe too disguised) point as I saw fit.Ryan Binns wrote:
That wasn't what he asked at all
Since when is there a one-to-one relationship between what one asks and what one means? ;) -- The Blog: Bits and Pieces
-
Johann Gerell wrote:
because you're a conscious developer
Well, I certainly hope he's conscious when he codes. Quite a feat to code while unconscious (although looking at some code makes one wonder:~ ). Or did you mean conscientious?
You may be right I may be crazy But it just may be a lunatic you’re looking for -- Billy Joel -- Within you lies the power for good - Use it!
PJ Arends wrote:
Or did you mean conscientious?
Check! (The reply is now somewhat updated... ;)) -- The Blog: Bits and Pieces
-
Ryan Binns wrote:
You assume too much.
That assumption was more of a veiled desire. I could have replied: "If you're using
char[]
instead ofstd::string
, then I'll be more than willing to answer your question if you restate it using astd::string
instead." But that would've been a bit too pompous, wouldn't it? So, I stuck in my (maybe too disguised) point as I saw fit.Ryan Binns wrote:
That wasn't what he asked at all
Since when is there a one-to-one relationship between what one asks and what one means? ;) -- The Blog: Bits and Pieces
Johann Gerell wrote:
That assumption was more of a veiled desire.
You're still assuming too much about his problem. Is he actually writing C++, or maintaining (or even writing) an ANSI C program? He hasn't told us. Your solution, while excellent for C++, is not very useful for a C program.
Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
-
Hi, I want to concatenate a variable of type 'unsigned long' to a variable of type 'char []'. How can this be done, since strcat() function takes 'char' arguments? Thanks. -Kranti
Here's one way:
char szBuff[5] = {0};
_ultoa(1234, szBuff, 10);
char szTemp[32];
strcpy(szTemp, "The number is ");
strcat(szTemp, szBuff);
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb