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. How to extract integer value from CString

How to extract integer value from CString

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
10 Posts 8 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.
  • R Offline
    R Offline
    Ravi Sankar S
    wrote on last edited by
    #1

    Hello All, I have to get integer value which is stored in a CString object. My code CString str = "-123"; int i; sscanf (str, "%d", &i); This is what i followed. Is there any better approach than this? Thanks for your time Ravi

    W V 2 Replies Last reply
    0
    • R Ravi Sankar S

      Hello All, I have to get integer value which is stored in a CString object. My code CString str = "-123"; int i; sscanf (str, "%d", &i); This is what i followed. Is there any better approach than this? Thanks for your time Ravi

      W Offline
      W Offline
      wicked_guy
      wrote on last edited by
      #2

      Try this Ravi: CString str = "-123"; int i = atoi((LPCSTR)str); If it didn't work, simply remove '(LPCSTR)'

      R 1 Reply Last reply
      0
      • R Ravi Sankar S

        Hello All, I have to get integer value which is stored in a CString object. My code CString str = "-123"; int i; sscanf (str, "%d", &i); This is what i followed. Is there any better approach than this? Thanks for your time Ravi

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

        Hi Ravi, We have ready made function for data conversion. You can use the following function to convert CString to int. CString str = "-123"; int i = atoi(str); This may be helpful you. Regards, Venkat

        M T 2 Replies Last reply
        0
        • V vensatren

          Hi Ravi, We have ready made function for data conversion. You can use the following function to convert CString to int. CString str = "-123"; int i = atoi(str); This may be helpful you. Regards, Venkat

          M Offline
          M Offline
          maheswara
          wrote on last edited by
          #4

          we can use that CString class method such as string Format CString str = " "; int i; i = str.Format("%d",str);

          2 T 2 Replies Last reply
          0
          • M maheswara

            we can use that CString class method such as string Format CString str = " "; int i; i = str.Format("%d",str);

            2 Offline
            2 Offline
            224917
            wrote on last edited by
            #5

            maheswara wrote: i = str.Format("%d",str); CString::Format() dosen't return any value.


            suhredayan
            There is no spoon.

            M 1 Reply Last reply
            0
            • V vensatren

              Hi Ravi, We have ready made function for data conversion. You can use the following function to convert CString to int. CString str = "-123"; int i = atoi(str); This may be helpful you. Regards, Venkat

              T Offline
              T Offline
              toxcct
              wrote on last edited by
              #6

              Venkat Bodapati wrote: We have ready made function for data conversion. [...] int i = atoi(str); :laugh::laugh: you wrote atoi() ??? wow, great... i'd be very curious to see the source (YOUR source) ... !


              TOXCCT >>> GEII power
              [toxcct][VisualCalc]

              1 Reply Last reply
              0
              • M maheswara

                we can use that CString class method such as string Format CString str = " "; int i; i = str.Format("%d",str);

                T Offline
                T Offline
                toxcct
                wrote on last edited by
                #7

                no, CString::Format() only formats a string with some parameters to put into the CString object... there's no function into the CString class to make the opposite operation. moreover, here is what MSDN tells us :

                void __cdecl Format(
                UINT nFormatID,
                [, argument]...
                );
                void __cdecl Format(
                PCXSTR pszFormat,
                [, argument]...
                );

                CString::Format() don't return anything !!


                TOXCCT >>> GEII power
                [toxcct][VisualCalc]

                1 Reply Last reply
                0
                • 2 224917

                  maheswara wrote: i = str.Format("%d",str); CString::Format() dosen't return any value.


                  suhredayan
                  There is no spoon.

                  M Offline
                  M Offline
                  maheswara
                  wrote on last edited by
                  #8

                  yeh it does not any return any value

                  A 1 Reply Last reply
                  0
                  • M maheswara

                    yeh it does not any return any value

                    A Offline
                    A Offline
                    Anonymous
                    wrote on last edited by
                    #9

                    maheswara wrote: yeh it does not any return any value So your following suggestion : i = str.Format("%d",str); is not correct and CString::Format function is not for converting a string to integer, though you can use it for the reverse purpose.

                    1 Reply Last reply
                    0
                    • W wicked_guy

                      Try this Ravi: CString str = "-123"; int i = atoi((LPCSTR)str); If it didn't work, simply remove '(LPCSTR)'

                      R Offline
                      R Offline
                      Rick York
                      wrote on last edited by
                      #10

                      LPCSTR is not necessary. CString has a built-in operator that returns a LPCTSTR.

                      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