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. Converting a string value to int

Converting a string value to int

Scheduled Pinned Locked Moved C#
csharpquestionhelp
12 Posts 5 Posters 1 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.
  • D dashingsidds

    Hi Experts, I am trying to convert a VB application to C# .net. I came across an enum which contained values as stsAvailabilityBits = &HF, stsFormAccessAllowed = &H1, stsValidUser = &H2, stsSystemAvailable = &H4, stsPermissionGranted = &H8, stsInquiryOnlyBits = &HF0, stsFormInquiryOnly = &H10, stsUserInquiryOnly = &H20, stsSystemInquiryOnly = &H40, stsRecordLocked = &H80, stsShutdownMinutesBase = &H100, stsShutdownMinutes = &HF00, stsErrorReturned = &H1000, stsFatalError = &H2000, stsTableEmpty = &H4000, stsResultsReturned = &H8000, stsParentControl = &H10000, stsLoadResultset = &H20000, stsSpecialPrivBitsReturned = &H40000, stsRecordAccessReturned = &H80000, stsTableLockRequested = &H100000, stsTableLockConflict = &H200000, stsMultipleLockConflict = &H400000, stsAvailBit23 = &H800000, stsUserCanExport = &H1000000, stsUserCanRename = &H2000000, stsUserCanReport = &H4000000, stsUserCanDelete = &H8000000, stsGetForInquiry = &H10000000, stsWebUser = &H20000000, stsAvailBit30 = &H40000000, stsAvailBit31 = &H80000000 I am not able to understand what are these. I think they are hexadecimal values. If they are then how can i convert them into decimal/integer values in C#.net? Please help! Thanks! Regards, Samar

    E Offline
    E Offline
    Estys
    wrote on last edited by
    #3

    Hexadecimal, Decimal, Binary etc are representations, they're always good values. Replace "&H" by "0x". Cheers

    I don't like my signature at all

    D 1 Reply Last reply
    0
    • D dashingsidds

      Hi Experts, I am trying to convert a VB application to C# .net. I came across an enum which contained values as stsAvailabilityBits = &HF, stsFormAccessAllowed = &H1, stsValidUser = &H2, stsSystemAvailable = &H4, stsPermissionGranted = &H8, stsInquiryOnlyBits = &HF0, stsFormInquiryOnly = &H10, stsUserInquiryOnly = &H20, stsSystemInquiryOnly = &H40, stsRecordLocked = &H80, stsShutdownMinutesBase = &H100, stsShutdownMinutes = &HF00, stsErrorReturned = &H1000, stsFatalError = &H2000, stsTableEmpty = &H4000, stsResultsReturned = &H8000, stsParentControl = &H10000, stsLoadResultset = &H20000, stsSpecialPrivBitsReturned = &H40000, stsRecordAccessReturned = &H80000, stsTableLockRequested = &H100000, stsTableLockConflict = &H200000, stsMultipleLockConflict = &H400000, stsAvailBit23 = &H800000, stsUserCanExport = &H1000000, stsUserCanRename = &H2000000, stsUserCanReport = &H4000000, stsUserCanDelete = &H8000000, stsGetForInquiry = &H10000000, stsWebUser = &H20000000, stsAvailBit30 = &H40000000, stsAvailBit31 = &H80000000 I am not able to understand what are these. I think they are hexadecimal values. If they are then how can i convert them into decimal/integer values in C#.net? Please help! Thanks! Regards, Samar

      R Offline
      R Offline
      Rajesh Anuhya
      wrote on last edited by
      #4

      either u can use escape sequence like char ch='\xFF'; Cheers

      Rajesh B --> A Poor Workman Blames His Tools <--

      1 Reply Last reply
      0
      • I Ice_Freez05

        try this code...

        private string getDecodedKey(string strKey)
        {
            string strTemp = string.Empty;
            string\[\] strSplitKey = strKey.Split(' ');
        
            foreach (string strHex in strSplitKey)
            {
                try
                {
                        int intVal = Convert.ToInt32(strHex, 16);
        
                        strTemp = strTemp + Char.ConvertFromUtf32(intVal);
        
                }
                catch
                {
                    strTemp = strTemp + strHex + "GET DECODED KEY";
                }
        
            }
            return strTemp;
        }
        
        D Offline
        D Offline
        dashingsidds
        wrote on last edited by
        #5

        Hi Ice, I tried running your code with the value of strKey as "&HF" and it showed me the error "Could not find any recognizable digits." on this line of code int intVal = Convert.ToInt32(strHex, 16);. Am i doing anything wrong here? Regards, Samar

        1 Reply Last reply
        0
        • E Estys

          Hexadecimal, Decimal, Binary etc are representations, they're always good values. Replace "&H" by "0x". Cheers

          I don't like my signature at all

          D Offline
          D Offline
          dashingsidds
          wrote on last edited by
          #6

          Hi Estys,

          Estys wrote:

          Replace "&H" by "0x".

          What am i suppose to do after replacing? Because even after replacing it wont be an integer or float or decimal, right?? Am i suppose to try and convert into an int or something after that?? Regards, Samar

          E 1 Reply Last reply
          0
          • D dashingsidds

            Hi Estys,

            Estys wrote:

            Replace "&H" by "0x".

            What am i suppose to do after replacing? Because even after replacing it wont be an integer or float or decimal, right?? Am i suppose to try and convert into an int or something after that?? Regards, Samar

            E Offline
            E Offline
            Estys
            wrote on last edited by
            #7

            (vb) &HFF = (c#) 0xFF = 255 The values are the same, it's just another way of representation. The only thing that matters is the type of the variable. So int myint = 0xFF; is perfectly legal. The following lines are equivalent integer declaration / assigments:

            int sixteen = 16;
            int sixteen = 0x10;

            This is a float :

            float onetwethreefourfive = 123.45f;

            So you don't need to convert the value, just replace the hex-prefix from &H to 0x. In your case, some of the values cannot be assigned to int but must be assigned to uint or long, for instance 0x80000000.

            I don't like my signature at all

            modified on Tuesday, April 27, 2010 5:28 AM

            D 1 Reply Last reply
            0
            • E Estys

              (vb) &HFF = (c#) 0xFF = 255 The values are the same, it's just another way of representation. The only thing that matters is the type of the variable. So int myint = 0xFF; is perfectly legal. The following lines are equivalent integer declaration / assigments:

              int sixteen = 16;
              int sixteen = 0x10;

              This is a float :

              float onetwethreefourfive = 123.45f;

              So you don't need to convert the value, just replace the hex-prefix from &H to 0x. In your case, some of the values cannot be assigned to int but must be assigned to uint or long, for instance 0x80000000.

              I don't like my signature at all

              modified on Tuesday, April 27, 2010 5:28 AM

              D Offline
              D Offline
              dashingsidds
              wrote on last edited by
              #8

              Hey Estys, I got it! Thanks a lot! Just one more question. I have a textbox in which I am entering the data like "0xF". On click of a button i want to display its integer value. When i try the following code Convert.ToInt32(txtHex.Text); It is showing me "string not in correct format.". What am i missing here? Regards, Samar

              E 1 Reply Last reply
              0
              • D dashingsidds

                Hey Estys, I got it! Thanks a lot! Just one more question. I have a textbox in which I am entering the data like "0xF". On click of a button i want to display its integer value. When i try the following code Convert.ToInt32(txtHex.Text); It is showing me "string not in correct format.". What am i missing here? Regards, Samar

                E Offline
                E Offline
                Estys
                wrote on last edited by
                #9

                There :

                (Convert.ToInt32(txtHex.Text, 16)).ToString();

                I don't like my signature at all

                D 1 Reply Last reply
                0
                • E Estys

                  There :

                  (Convert.ToInt32(txtHex.Text, 16)).ToString();

                  I don't like my signature at all

                  D Offline
                  D Offline
                  dashingsidds
                  wrote on last edited by
                  #10

                  That is good Estys. Thanks a million. Regards, Samar

                  L 1 Reply Last reply
                  0
                  • I Ice_Freez05

                    try this code...

                    private string getDecodedKey(string strKey)
                    {
                        string strTemp = string.Empty;
                        string\[\] strSplitKey = strKey.Split(' ');
                    
                        foreach (string strHex in strSplitKey)
                        {
                            try
                            {
                                    int intVal = Convert.ToInt32(strHex, 16);
                    
                                    strTemp = strTemp + Char.ConvertFromUtf32(intVal);
                    
                            }
                            catch
                            {
                                strTemp = strTemp + strHex + "GET DECODED KEY";
                            }
                    
                        }
                        return strTemp;
                    }
                    
                    D Offline
                    D Offline
                    dashingsidds
                    wrote on last edited by
                    #11

                    Hi Ice, Your code was good except that i need to pass 0xF instead of &HF i.e. replacing &H with 0x. Also i am not getting the proper value when i use the following code but when i comment the same then it was working fine.

                    strTemp = strTemp + Char.ConvertFromUtf32(intVal);

                    Can you please let me know why were you using the above lines of code?? Regards, Samar

                    1 Reply Last reply
                    0
                    • D dashingsidds

                      That is good Estys. Thanks a million. Regards, Samar

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #12

                      No it is not so good; it may preserve the value, however the readability is gone. These numbers look like special bit patterns, so they should remain in hex. All you need is a syntax change, from VB syntax (&H12) to C# syntax (0x12).

                      enum MyEnumType {
                      stsAvailabilityBits = 0xF,
                      stsFormAccessAllowed = 0x1,
                      ...
                      }

                      :)

                      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                      Prolific encyclopedia fixture proof-reader browser patron addict?
                      We all depend on the beast below.


                      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