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 to Number

String to Number

Scheduled Pinned Locked Moved C / C++ / MFC
question
12 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.
  • W Waldermort

    look at the two functions atoi() itoa() I maybe wrong but I think the atoi() function will only convert the numerical characters from the string, and skip over the non-numerical. I don't use MFC myself, but there maybe a better way to do this using the MFC classes, I will leave that to the MFC guru's.

    C Offline
    C Offline
    color Aljechin
    wrote on last edited by
    #3

    I know its the CRT function atoi(). But its not working for some reason. Thats why I asked for a code snippet. Someone please helpme out. Aljechin

    W 2 Replies Last reply
    0
    • C color Aljechin

      I know its the CRT function atoi(). But its not working for some reason. Thats why I asked for a code snippet. Someone please helpme out. Aljechin

      W Offline
      W Offline
      Waldermort
      wrote on last edited by
      #4

      perhaps you could post the code you have created, this would give us a better idea of what you are trying and where you may be going wrong.

      1 Reply Last reply
      0
      • C color Aljechin

        I have one CString which contains "MX1200" as its value. I want 1200 from this and I want to store it in a integer variable. How do I do this? Can you show a small code snippet? Aljechin Alexander

        A Offline
        A Offline
        Aqueel
        wrote on last edited by
        #5

        Here is a way to do that. 1. Get length of your CString. 2. Declare a char array of that length and copy your CString to that array. 3. Now the number can easily be extracted from that array. Here is the code to copy CString to char array int size = myCString.GetLength(); char *array =(char*)malloc(size + 1); strcpy(array, myCString); If problem remains, do let us know. Good Luck We Believe in Excellence www.aqueelmirza.cjb.net

        1 Reply Last reply
        0
        • C color Aljechin

          I know its the CRT function atoi(). But its not working for some reason. Thats why I asked for a code snippet. Someone please helpme out. Aljechin

          W Offline
          W Offline
          Waldermort
          wrote on last edited by
          #6

          Ok, I just tested what I thought and I was half right. atoi will stop at the first non-numerical char, so if your string start with letter you have to remove them.

          char \*txt = "MX2001";
          char \*buffer = new char\[10\];
          int pos = 0;
          int result;
          // strip the non-numerical chars
          for (int i=0;i<strlen(txt);i++) {
              if ((txt\[i\] > 0x29)&&(txt\[i\] < 0x40)) { // if numerical
                  buffer\[pos\] = txt\[i\];
                  pos++;
              }
          }
          buffer\[pos\] = 0x00; // add a terminating null
          result = atoi(buffer);
          

          Or if your string is fairly constant and always the first two are letter you could

          result = atoi(&txt[2]);

          -- modified at 1:50 Wednesday 22nd March, 2006 Don't forget to use delete or you will have memory leaks;

          1 Reply Last reply
          0
          • C color Aljechin

            I have one CString which contains "MX1200" as its value. I want 1200 from this and I want to store it in a integer variable. How do I do this? Can you show a small code snippet? Aljechin Alexander

            C Offline
            C Offline
            color Aljechin
            wrote on last edited by
            #7

            Thank you all. Actually I had been a little dumb. I forgot I was doing a unicode build and should have used _wtoi function and not the atoi function :D. Thanks for all of your kind replies. Aljechin

            N A 2 Replies Last reply
            0
            • C color Aljechin

              Thank you all. Actually I had been a little dumb. I forgot I was doing a unicode build and should have used _wtoi function and not the atoi function :D. Thanks for all of your kind replies. Aljechin

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

              Aljechin should have used _wtoi function and not the atoi function :D.

              Always try to use tchar version of functions. Like _ttoi. This way you don't have to worry whether it's unicode or not.


              Nibu thomas Software Developer

              W 1 Reply Last reply
              0
              • N Nibu babu thomas

                Aljechin should have used _wtoi function and not the atoi function :D.

                Always try to use tchar version of functions. Like _ttoi. This way you don't have to worry whether it's unicode or not.


                Nibu thomas Software Developer

                W Offline
                W Offline
                Waldermort
                wrote on last edited by
                #9

                Your welcome. I wish more people would say thankyou after they have been helped and more people to state what they did wrong or how they fixed the problem. The message boards (not only this site) are filled with questions and no answers or gratitude. It makes searching for a solution a hard task.

                1 Reply Last reply
                0
                • C color Aljechin

                  Thank you all. Actually I had been a little dumb. I forgot I was doing a unicode build and should have used _wtoi function and not the atoi function :D. Thanks for all of your kind replies. Aljechin

                  A Offline
                  A Offline
                  Aqueel
                  wrote on last edited by
                  #10

                  You can always rank helpful replies (bottom right) We Believe in Excellence www.aqueelmirza.cjb.net -- modified at 3:25 Wednesday 22nd March, 2006

                  1 Reply Last reply
                  0
                  • C color Aljechin

                    I have one CString which contains "MX1200" as its value. I want 1200 from this and I want to store it in a integer variable. How do I do this? Can you show a small code snippet? Aljechin Alexander

                    L Offline
                    L Offline
                    lawrence2000
                    wrote on last edited by
                    #11

                    CString str=_T("MX1200" ); int position; for(int i=0 , str.length(),i++) if (str.getat(i) <= '9') and (str.getat(i) >= '0') { position =i; //exit; } str =str.left(position); int yourInteger =atoi(LPCTSTR(str));

                    1 Reply Last reply
                    0
                    • C color Aljechin

                      I have one CString which contains "MX1200" as its value. I want 1200 from this and I want to store it in a integer variable. How do I do this? Can you show a small code snippet? Aljechin Alexander

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

                      maybe it is some helpful to you http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_data_conversion.asp[^]

                      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