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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Converting int to CString

Converting int to CString

Scheduled Pinned Locked Moved C / C++ / MFC
comdata-structureshelptutorialquestion
5 Posts 3 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.
  • C Offline
    C Offline
    Cam
    wrote on last edited by
    #1

    First, a big "thank you" to everyone who has been helping me. I know I've been asking a lot of stupid questions, but once I get this stuff relearned, I think I'll be fine. So, here's the next stupid question. I've got an integer, and and I want it in a CString. For example: //Before int myint = 123; //After CString mystring = "123"; The problem is that when I try to assign it directly, it puts the character with the ASCII value of the int into the string (I think). Anyway, then I looked up itoa in MSDN, and found a solution. But you must pass itoa a character array to store the resulting text in. Anyway, the only way I know to make it work is like this: char cDay[3] = " "; _itoa(theTime.GetDay(), cDay, 10); CString Day = cDay; Anyway, the basic idea is that I changed it from an int to a char array, and then from a char array to a CString. But I'm sure there's a way to leave out the char array and just do it directly. Thanks again. ~Cam Desautels (BinaryUprising.com)

    J A 2 Replies Last reply
    0
    • C Cam

      First, a big "thank you" to everyone who has been helping me. I know I've been asking a lot of stupid questions, but once I get this stuff relearned, I think I'll be fine. So, here's the next stupid question. I've got an integer, and and I want it in a CString. For example: //Before int myint = 123; //After CString mystring = "123"; The problem is that when I try to assign it directly, it puts the character with the ASCII value of the int into the string (I think). Anyway, then I looked up itoa in MSDN, and found a solution. But you must pass itoa a character array to store the resulting text in. Anyway, the only way I know to make it work is like this: char cDay[3] = " "; _itoa(theTime.GetDay(), cDay, 10); CString Day = cDay; Anyway, the basic idea is that I changed it from an int to a char array, and then from a char array to a CString. But I'm sure there's a way to leave out the char array and just do it directly. Thanks again. ~Cam Desautels (BinaryUprising.com)

      J Offline
      J Offline
      Joaquin M Lopez Munoz
      wrote on last edited by
      #2

      The following snippet of code is useful for writing numeric values into std::strings:

      #include <sstream>
      template <typename T> inline std::string tostring(const T& t)
      {
      std::ostringstream ostr;
      ostr<<t;
      return ostr.str();
      }

      It must be trivial to adapt it for MFC CStrings. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

      C 1 Reply Last reply
      0
      • J Joaquin M Lopez Munoz

        The following snippet of code is useful for writing numeric values into std::strings:

        #include <sstream>
        template <typename T> inline std::string tostring(const T& t)
        {
        std::ostringstream ostr;
        ostr<<t;
        return ostr.str();
        }

        It must be trivial to adapt it for MFC CStrings. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

        C Offline
        C Offline
        Cam
        wrote on last edited by
        #3

        Ahh...yes, if I'm going to be doing a lot of conversions like that, a template would be a very good idea. But I don't really understand that code...I don't get what the binary shift does...but isn't there a more direct route? ~Cam Desautels (BinaryUprising.com)

        1 Reply Last reply
        0
        • C Cam

          First, a big "thank you" to everyone who has been helping me. I know I've been asking a lot of stupid questions, but once I get this stuff relearned, I think I'll be fine. So, here's the next stupid question. I've got an integer, and and I want it in a CString. For example: //Before int myint = 123; //After CString mystring = "123"; The problem is that when I try to assign it directly, it puts the character with the ASCII value of the int into the string (I think). Anyway, then I looked up itoa in MSDN, and found a solution. But you must pass itoa a character array to store the resulting text in. Anyway, the only way I know to make it work is like this: char cDay[3] = " "; _itoa(theTime.GetDay(), cDay, 10); CString Day = cDay; Anyway, the basic idea is that I changed it from an int to a char array, and then from a char array to a CString. But I'm sure there's a way to leave out the char array and just do it directly. Thanks again. ~Cam Desautels (BinaryUprising.com)

          A Offline
          A Offline
          Alvaro Mendez
          wrote on last edited by
          #4

          Here's the most direct way, short of writing your own little function to do it:

          CString Day;
          Day.Format(_T("%d"), theTime.GetDay());

          Regards, Alvaro

          C 1 Reply Last reply
          0
          • A Alvaro Mendez

            Here's the most direct way, short of writing your own little function to do it:

            CString Day;
            Day.Format(_T("%d"), theTime.GetDay());

            Regards, Alvaro

            C Offline
            C Offline
            Cam
            wrote on last edited by
            #5

            Ahhh :), thank you. ~Cam Desautels (BinaryUprising.com)

            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