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. How to convert CString to int MFC 7.1

How to convert CString to int MFC 7.1

Scheduled Pinned Locked Moved C / C++ / MFC
7 Posts 6 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.
  • G Offline
    G Offline
    Greg Ellis
    wrote on last edited by
    #1

    Hi Guys, I am trying to convert a CString to an int in an MFC 7.1 unicode project, but it wont work. Here is what I tried. CString cszNumber = "1234567"; int nNumber = atoi(cszNumber); That didn't compile so I tried this CString cszNumber = "1234567"; int nNumber = _ttoi(cszNumber.GetBuffer(0)); That only brought across the first digit. Any Ideas? This method used to work fine in my old non unicode MFC 6 project. Cheers, Greg

    RaviBeeR C H M 4 Replies Last reply
    0
    • G Greg Ellis

      Hi Guys, I am trying to convert a CString to an int in an MFC 7.1 unicode project, but it wont work. Here is what I tried. CString cszNumber = "1234567"; int nNumber = atoi(cszNumber); That didn't compile so I tried this CString cszNumber = "1234567"; int nNumber = _ttoi(cszNumber.GetBuffer(0)); That only brought across the first digit. Any Ideas? This method used to work fine in my old non unicode MFC 6 project. Cheers, Greg

      RaviBeeR Offline
      RaviBeeR Offline
      RaviBee
      wrote on last edited by
      #2

      Here's one way. Be sure to check the return status of sscanf_s().

      int nNumber = 0;
      CString strNumber = "1234567";
      sscanf_s (strNumber, "%d", &nNumber);

      /ravi

      This is your brain on Celcius Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com

      PJ ArendsP 1 Reply Last reply
      0
      • G Greg Ellis

        Hi Guys, I am trying to convert a CString to an int in an MFC 7.1 unicode project, but it wont work. Here is what I tried. CString cszNumber = "1234567"; int nNumber = atoi(cszNumber); That didn't compile so I tried this CString cszNumber = "1234567"; int nNumber = _ttoi(cszNumber.GetBuffer(0)); That only brought across the first digit. Any Ideas? This method used to work fine in my old non unicode MFC 6 project. Cheers, Greg

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        _ttoi should work without the GetBuffer call, I believe.

        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

        1 Reply Last reply
        0
        • RaviBeeR RaviBee

          Here's one way. Be sure to check the return status of sscanf_s().

          int nNumber = 0;
          CString strNumber = "1234567";
          sscanf_s (strNumber, "%d", &nNumber);

          /ravi

          This is your brain on Celcius Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com

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

          Why use scanf? I always prefer to use strtol to accomplish this task. I personally hate scanf so I was wondering why you would recommend it.


          You may be right
          I may be crazy
          -- Billy Joel --

          Within you lies the power for good, use it!!!

          Within you lies the power for good; Use it!

          RaviBeeR 1 Reply Last reply
          0
          • PJ ArendsP PJ Arends

            Why use scanf? I always prefer to use strtol to accomplish this task. I personally hate scanf so I was wondering why you would recommend it.


            You may be right
            I may be crazy
            -- Billy Joel --

            Within you lies the power for good, use it!!!

            RaviBeeR Offline
            RaviBeeR Offline
            RaviBee
            wrote on last edited by
            #5

            PJ Arends wrote:

            Why use scanf?

            (s)scanf() is deterministic - strtol() isn't. Nor is _ttoi() as suggested by CG. /ravi

            This is your brain on Celcius Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com

            1 Reply Last reply
            0
            • G Greg Ellis

              Hi Guys, I am trying to convert a CString to an int in an MFC 7.1 unicode project, but it wont work. Here is what I tried. CString cszNumber = "1234567"; int nNumber = atoi(cszNumber); That didn't compile so I tried this CString cszNumber = "1234567"; int nNumber = _ttoi(cszNumber.GetBuffer(0)); That only brought across the first digit. Any Ideas? This method used to work fine in my old non unicode MFC 6 project. Cheers, Greg

              H Offline
              H Offline
              Hamid Taebi
              wrote on last edited by
              #6

              And one more thing when use of GetBuffer use of ReleaseBuffer ;)


              WhiteSky


              1 Reply Last reply
              0
              • G Greg Ellis

                Hi Guys, I am trying to convert a CString to an int in an MFC 7.1 unicode project, but it wont work. Here is what I tried. CString cszNumber = "1234567"; int nNumber = atoi(cszNumber); That didn't compile so I tried this CString cszNumber = "1234567"; int nNumber = _ttoi(cszNumber.GetBuffer(0)); That only brought across the first digit. Any Ideas? This method used to work fine in my old non unicode MFC 6 project. Cheers, Greg

                M Offline
                M Offline
                Mark Salsbery
                wrote on last edited by
                #7

                Or you could use the "Generic-Text Routine Mappings" These will work on both Unicode and non-Unicode builds. CString cszNumber = _T("1234567"); int nNumber = _tstoi(cszNumber);

                "Great job, team. Head back to base for debriefing and cocktails." (Spottswoode "Team America")

                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