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. CString to Numeric Value

CString to Numeric Value

Scheduled Pinned Locked Moved C / C++ / MFC
tutorial
9 Posts 4 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.
  • S Offline
    S Offline
    si_69
    wrote on last edited by
    #1

    Hi All does any one know what would be the best way to convert a CString containing only characters to a numeric value. for example CString str = "AFG" output to be say int 653790; and each time you convert the str, you get the same result Thanks Si :)

    N M 2 Replies Last reply
    0
    • S si_69

      Hi All does any one know what would be the best way to convert a CString containing only characters to a numeric value. for example CString str = "AFG" output to be say int 653790; and each time you convert the str, you get the same result Thanks Si :)

      N Offline
      N Offline
      Nirav Doshi
      wrote on last edited by
      #2

      si_69 wrote: CString str = "AFG" output to be say int 653790; Do you want a hash value of the string, or simply want each character in the CString to be converted to its ASCII value? * Artificial intelligence is no match for natural stupidity! *

      S 1 Reply Last reply
      0
      • S si_69

        Hi All does any one know what would be the best way to convert a CString containing only characters to a numeric value. for example CString str = "AFG" output to be say int 653790; and each time you convert the str, you get the same result Thanks Si :)

        M Offline
        M Offline
        Manfred Staiger
        wrote on last edited by
        #3

        Did you try atoi or _atoi64 ? MS

        S 1 Reply Last reply
        0
        • N Nirav Doshi

          si_69 wrote: CString str = "AFG" output to be say int 653790; Do you want a hash value of the string, or simply want each character in the CString to be converted to its ASCII value? * Artificial intelligence is no match for natural stupidity! *

          S Offline
          S Offline
          si_69
          wrote on last edited by
          #4

          Nirav Doshi wrote: Do you want a hash value of the string, or simply want each character in the CString to be converted to its ASCII value? yeah i would need for each character in the CString to be converted to its ASCII values thanks

          N 1 Reply Last reply
          0
          • M Manfred Staiger

            Did you try atoi or _atoi64 ? MS

            S Offline
            S Offline
            si_69
            wrote on last edited by
            #5

            Manfred Staiger wrote: Did you try atoi or _atoi64 ? yes, both return 0 i have lots of character ID's such as 'AB', 'POL', 'AA' etc i need to convert them to unique values

            M 1 Reply Last reply
            0
            • S si_69

              Manfred Staiger wrote: Did you try atoi or _atoi64 ? yes, both return 0 i have lots of character ID's such as 'AB', 'POL', 'AA' etc i need to convert them to unique values

              M Offline
              M Offline
              Manfred Staiger
              wrote on last edited by
              #6

              What about converting each character of the string to it's ASCII code (__toascii) assemble the results to a number ? I hope you understand what I mean, my english is not the best. MS

              1 Reply Last reply
              0
              • S si_69

                Nirav Doshi wrote: Do you want a hash value of the string, or simply want each character in the CString to be converted to its ASCII value? yeah i would need for each character in the CString to be converted to its ASCII values thanks

                N Offline
                N Offline
                Nirav Doshi
                wrote on last edited by
                #7

                A simple function would do for that!

                CString GetASCIICodedString(CString& srcString)
                {
                    CString srcResult, srcBuffer;
                
                    for(int nIndex = 0; nIndex < srcString.GetLength(); nIndex++)
                    {
                        srcBuffer.Empty();
                        srcBuffer.Format("%d", srcString.GetAt(nIndex));
                        srcResult += srcBuffer;
                    }
                
                    return srcResult;
                }
                

                This should work... I've not compiled it, just some rough code, so please test it properly. * Artificial intelligence is no match for natural stupidity! *

                N R 2 Replies Last reply
                0
                • N Nirav Doshi

                  A simple function would do for that!

                  CString GetASCIICodedString(CString& srcString)
                  {
                      CString srcResult, srcBuffer;
                  
                      for(int nIndex = 0; nIndex < srcString.GetLength(); nIndex++)
                      {
                          srcBuffer.Empty();
                          srcBuffer.Format("%d", srcString.GetAt(nIndex));
                          srcResult += srcBuffer;
                      }
                  
                      return srcResult;
                  }
                  

                  This should work... I've not compiled it, just some rough code, so please test it properly. * Artificial intelligence is no match for natural stupidity! *

                  N Offline
                  N Offline
                  Nirav Doshi
                  wrote on last edited by
                  #8

                  Is possible, please let me know if this helped, or if it gave you any further problems! * Artificial intelligence is no match for natural stupidity! *

                  1 Reply Last reply
                  0
                  • N Nirav Doshi

                    A simple function would do for that!

                    CString GetASCIICodedString(CString& srcString)
                    {
                        CString srcResult, srcBuffer;
                    
                        for(int nIndex = 0; nIndex < srcString.GetLength(); nIndex++)
                        {
                            srcBuffer.Empty();
                            srcBuffer.Format("%d", srcString.GetAt(nIndex));
                            srcResult += srcBuffer;
                        }
                    
                        return srcResult;
                    }
                    

                    This should work... I've not compiled it, just some rough code, so please test it properly. * Artificial intelligence is no match for natural stupidity! *

                    R Offline
                    R Offline
                    Ravi Bhavnani
                    wrote on last edited by
                    #9

                    srcBuffer.Empty(); seems extraneous. /ravi My new year's resolution: 2048 x 1536 Home | Articles | Freeware | Music ravib@ravib.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