Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. string concatenation

string concatenation

Scheduled Pinned Locked Moved C / C++ / MFC
question
10 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kranti1251984
    wrote on last edited by
    #1

    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

    N V J D 4 Replies Last reply
    0
    • K Kranti1251984

      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

      N Offline
      N Offline
      Nibu babu thomas
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • K Kranti1251984

        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

        V Offline
        V Offline
        Vinaya
        wrote on last edited by
        #3

        You can use sprintf(). check here ... [^] Vini

        1 Reply Last reply
        0
        • K Kranti1251984

          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

          J Offline
          J Offline
          Johann Gerell
          wrote on last edited by
          #4

          Assuming that you by char[] really mean std::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

          R PJ ArendsP 2 Replies Last reply
          0
          • J Johann Gerell

            Assuming that you by char[] really mean std::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

            R Offline
            R Offline
            Ryan Binns
            wrote on last edited by
            #5

            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"

            J 1 Reply Last reply
            0
            • J Johann Gerell

              Assuming that you by char[] really mean std::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

              PJ ArendsP Offline
              PJ ArendsP Offline
              PJ Arends
              wrote on last edited by
              #6

              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!

              Within you lies the power for good; Use it!

              J 1 Reply Last reply
              0
              • R Ryan Binns

                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"

                J Offline
                J Offline
                Johann Gerell
                wrote on last edited by
                #7

                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 of std::string, then I'll be more than willing to answer your question if you restate it using a std::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

                R 1 Reply Last reply
                0
                • PJ ArendsP PJ Arends

                  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!

                  J Offline
                  J Offline
                  Johann Gerell
                  wrote on last edited by
                  #8

                  PJ Arends wrote:

                  Or did you mean conscientious?

                  Check! (The reply is now somewhat updated... ;)) -- The Blog: Bits and Pieces

                  1 Reply Last reply
                  0
                  • J Johann Gerell

                    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 of std::string, then I'll be more than willing to answer your question if you restate it using a std::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

                    R Offline
                    R Offline
                    Ryan Binns
                    wrote on last edited by
                    #9

                    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"

                    1 Reply Last reply
                    0
                    • K Kranti1251984

                      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

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #10

                      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

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups