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#
  4. string to int

string to int

Scheduled Pinned Locked Moved C#
tutorialquestion
12 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.
  • T Offline
    T Offline
    tomiga
    wrote on last edited by
    #1

    How to convert? (varius methods if possible, please)

    T 2 Replies Last reply
    0
    • T tomiga

      How to convert? (varius methods if possible, please)

      T Offline
      T Offline
      Tom Archer
      wrote on last edited by
      #2

      Using atoi will work: CString str = "42"; int i = atoi(str); Cheers, Tom Archer Author, Inside C# A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the affairs of others.

      T 1 Reply Last reply
      0
      • T tomiga

        How to convert? (varius methods if possible, please)

        T Offline
        T Offline
        Tom Archer
        wrote on last edited by
        #3

        OOOPS!!!!! I've been in the MFC forum all day and my head is still in that :) Use the Convert static functions to do what you want.

        string s = "42";
        int i = Convert.ToInt32(s);

        Cheers, Tom Archer Author, Inside C# A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the affairs of others.

        E T 2 Replies Last reply
        0
        • T Tom Archer

          OOOPS!!!!! I've been in the MFC forum all day and my head is still in that :) Use the Convert static functions to do what you want.

          string s = "42";
          int i = Convert.ToInt32(s);

          Cheers, Tom Archer Author, Inside C# A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the affairs of others.

          E Offline
          E Offline
          ez2
          wrote on last edited by
          #4

          What's the difference between convert and parse. string s = "42"; int i = int32.parse(s);

          T J 2 Replies Last reply
          0
          • E ez2

            What's the difference between convert and parse. string s = "42"; int i = int32.parse(s);

            T Offline
            T Offline
            Tom Archer
            wrote on last edited by
            #5

            Parse is specific to the Int32 class while Convert is a generic class supporting all types. Cheers, Tom Archer Author, Inside C# A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the affairs of others.

            1 Reply Last reply
            0
            • E ez2

              What's the difference between convert and parse. string s = "42"; int i = int32.parse(s);

              J Offline
              J Offline
              James T Johnson
              wrote on last edited by
              #6

              It should also be noted that in the case of converting a string to an int via the Convert class it calls Int32.Parse. James Simplicity Rules!

              T 1 Reply Last reply
              0
              • J James T Johnson

                It should also be noted that in the case of converting a string to an int via the Convert class it calls Int32.Parse. James Simplicity Rules!

                T Offline
                T Offline
                Tom Archer
                wrote on last edited by
                #7

                Good point, James. I'm so accustomed to using the Convert class for everything that I never think about Parse. Cheers, Tom Archer Author, Inside C# A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the affairs of others.

                1 Reply Last reply
                0
                • T Tom Archer

                  Using atoi will work: CString str = "42"; int i = atoi(str); Cheers, Tom Archer Author, Inside C# A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the affairs of others.

                  T Offline
                  T Offline
                  tomiga
                  wrote on last edited by
                  #8

                  it would be cool ;)

                  T 1 Reply Last reply
                  0
                  • T tomiga

                    it would be cool ;)

                    T Offline
                    T Offline
                    Tom Archer
                    wrote on last edited by
                    #9

                    You might even be able to do this through PInvoke, but I wouldn't necessarily recommend it :) I think I'll stick with "What is Convert?", Alex :laugh: Cheers, Tom Archer Author, Inside C# A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the affairs of others.

                    1 Reply Last reply
                    0
                    • T Tom Archer

                      OOOPS!!!!! I've been in the MFC forum all day and my head is still in that :) Use the Convert static functions to do what you want.

                      string s = "42";
                      int i = Convert.ToInt32(s);

                      Cheers, Tom Archer Author, Inside C# A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the affairs of others.

                      T Offline
                      T Offline
                      tomiga
                      wrote on last edited by
                      #10

                      thx... i was looking for this conversion in string class. As I can see there is special class to conversions.

                      E 1 Reply Last reply
                      0
                      • T tomiga

                        thx... i was looking for this conversion in string class. As I can see there is special class to conversions.

                        E Offline
                        E Offline
                        ez2
                        wrote on last edited by
                        #11

                        So I guess my followup question would be when to use one or the other (parse vs. convert). Also, is parse more efficient? Just looking for some guidelines here.

                        J 1 Reply Last reply
                        0
                        • E ez2

                          So I guess my followup question would be when to use one or the other (parse vs. convert). Also, is parse more efficient? Just looking for some guidelines here.

                          J Offline
                          J Offline
                          James T Johnson
                          wrote on last edited by
                          #12

                          Convert can convert from all intrinsic datatypes to the other types; parse only accepts strings. Convert also calls the appropriate parse method when converting a string to that format except when converting UInt32 which isn't CTS compliant and converting string to string where it just returns the value ;P. I would use Convert for consistancy in code, but in some cases, Parse will take a few extra parameters you may wish to use. HTH, James Simplicity Rules!

                          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