How to use the Int32.Parse method to convert the string representation of a hexadecimal integer to a decimal integer by using Visual C# .NET

Article translations Article translations
Article ID: 835454 - View products that this article applies to.
We strongly recommend that all users upgrade to Microsoft Internet Information Services (IIS) version 7.0 running on Microsoft Windows Server 2008. IIS 7.0 significantly increases Web infrastructure security. For more information about IIS security-related topics, visit the following Microsoft Web site:
http://technet.microsoft.com/en-us/library/dd450371.aspx
For more information about IIS 7.0, visit the following Microsoft Web site:
http://www.iis.net/default.aspx?tabid=1
Expand all | Collapse all

On This Page

SUMMARY

You can convert the string representation of a hexadecimal integer to a decimal integer by using an overloaded version of the Int32.Parse method.

The Microsoft .NET Framework includes four overloaded versions of the Int32.Parse method. By using Microsoft Visual C# .NET, you can pass an appropriate NumberStyles enumeration to use the Int32.Parse(String, NumberStyles) method to convert the string representation of a hexadecimal integer to a decimal integer.

You can use the Int32.Parse(String, NumberStyles) method to implement code to change the background color of a Microsoft Windows Form. If you use "0x" to specify a hexadecimal format to the Int32.Parse method, you may receive a "System.FormatException" error message.

INTRODUCTION

The step-by-step article describes how to use an overloaded version of the Int32.Parse method to convert the string representation of a hexadecimal integer to a decimal integer by using Visual C# .NET. This article contains code that uses this method to change the background color of a Windows Form.

Requirements

This article assumes that you are familiar with the following topics:
  • Programming by using Visual C# .NET
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
  • Microsoft Windows Server 2000, Windows XP, Windows Server 2003, or a later Microsoft operating system
  • Microsoft Visual Studio

The Int32.Parse method

To convert the string representation of a hexadecimal integer to a decimal integer, you can use the Int32.Parse method. This method converts the string representation of any number to its 32-bit signed integer equivalent. The .NET Framework provides the following four overloaded versions of the Int32.Parse method:
  • Int32.Parse(String)
  • Int32.Parse(String, IFormatProvider)
  • Int32.Parse(String, NumberStyles)
  • Int32.Parse(String, NumberStyles, IFormatProvider)
To convert the string representation of a hexadecimal integer to a decimal integer, you can use the Int32.Parse(String, NumberStyles) method. This version of the Int32.Parse method accepts a string and a number style. The string contains the string representation of any number in a style that you specify by using the number style.

The NumberStyles enumeration

The NumberStyles enumeration is included in the System.Globalization namespace. You can use this enumeration to specify the permitted styles in strings that you pass to the Int32.Parse(String, NumberStyles) method. The enumeration has a FlagsAttribute attribute that allows a bitwise combination of its member values. The most significant bit (MSB) of the NumberStyles enumeration is the leftmost bit.

Step-by-step sample

  1. Start Visual Studio .NET.
  2. On the File menu, point to New, and then click Project. The New Project dialog box appears.
  3. Under Project Types, click Visual C# Projects.
  4. Under Templates, click Windows Application.
  5. Click OK. By default, a file that is named Form1.cs is created.
  6. Right-click the Form1 Windows Form, and then click View Code.
  7. Replace the existing code with the following code.
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    
    namespace WindowsApplication1
    {
       /// <summary>
       /// Summary description for Form1.
       /// </summary>
       public class Form1 : System.Windows.Forms.Form
       {
          /// <summary>
          /// Required designer variable.
          /// </summary>
          private System.ComponentModel.Container components = null;
          public Form1()
          {
             //
             // Required for Windows Forms Designer support.
             //
             InitializeComponent();
    
             //
             // TODO: Add any constructor code after InitializeComponent call
             //
    
             this.BackColor= stringToColor("000000");
          }
    
          /// <summary>
          /// Clean up any resources that were being used.
          /// </summary>
          protected override void Dispose( bool disposing )
          {
             if( disposing )
             {
                if (components != null) 
                {
                   components.Dispose();
                }
             }
             base.Dispose( disposing );
          }
    
          #region Windows Form Designer generated code
          /// <summary>
          /// Required method for Designer support - do not modify
          /// the contents of this method by using the code editor.
          /// </summary>
          private void InitializeComponent()
          {
             // 
             // Form1
             // 
             this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
             this.BackColor = System.Drawing.SystemColors.Control;
             this.ClientSize = new System.Drawing.Size(292, 266);
             this.ForeColor = System.Drawing.SystemColors.Control;
             this.Name = "Form1";
             this.Text = "Form1";
          }
          #endregion
    
          /// <summary>
          /// The main entry point for the application.
          /// </summary>
          [STAThread]
          static void Main() 
          {
             Application.Run(new Form1());
          }
    
          private System.Drawing.Color stringToColor(System.String paramValue)
          {
             int red;
             int green;
             int blue;
             red = (System.Int32.Parse( paramValue.Substring(0, (2) - (0)),System.Globalization.NumberStyles.AllowHexSpecifier));
             green = (System.Int32.Parse(paramValue.Substring(2, (4) - (2)),System.Globalization.NumberStyles.AllowHexSpecifier));
             blue = (System.Int32.Parse(paramValue.Substring(4, (6) - (4)),System.Globalization.NumberStyles.AllowHexSpecifier));
             return System.Drawing.Color.FromArgb(red, green, blue);
          }
       }
    }
    Note Visual C# 2005 and Visual C# 2008 implement the Form1 class as a partial class in two source files. The project includes a Form1.cs file that contains user-modifiable code and a Form1.Designer.cs file whose code should be modified only by the designer. The following code is a complete listing of the Form1.cs file.
    using System.Windows.Forms;
    
    namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                this.BackColor = stringToColor("000000");
            }
    
            private System.Drawing.Color stringToColor(System.String paramValue)
            {
                int red;
                int green;
                int blue;
                red = (System.Int32.Parse(paramValue.Substring(0, (2) - (0)), System.Globalization.NumberStyles.AllowHexSpecifier));
                green = (System.Int32.Parse(paramValue.Substring(2, (4) - (2)), System.Globalization.NumberStyles.AllowHexSpecifier));
                blue = (System.Int32.Parse(paramValue.Substring(4, (6) - (4)), System.Globalization.NumberStyles.AllowHexSpecifier));
                return System.Drawing.Color.FromArgb(red, green, blue);
            }
        }
    }
    
  8. On the Build menu, click Build Solution.
  9. On the Debug menu, click Start to run the application. The Form1 Windows Form appears. The background color of the form is black.

Troubleshooting

If you pass a string to the Int32.Parse method, and the string uses "0x" to specify a hexadecimal format, a System.FormatException error may occur at run time.

REFERENCES

For more information, visit the following Microsoft Developer Network (MSDN) Web site:
http://msdn.microsoft.com/en-us/library/system.int32.parse.aspx

Properties

Article ID: 835454 - Last Review: May 17, 2007 - Revision: 1.7
APPLIES TO
  • Microsoft Visual C# 2008 Express Edition
  • Microsoft Visual Studio .NET 2003 Professional Edition
  • Microsoft Visual Studio .NET 2003 Enterprise Architect
  • Microsoft Visual Studio .NET 2003 Enterprise Developer
  • Microsoft Visual Studio .NET 2003 Academic Edition
  • Microsoft Visual C# .NET 2003 Standard Edition
Keywords: 
kbvs2002sp1sweep kbforms kbcolor kbuser kbappdev kbcode kbbug kbhowtomaster KB835454

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com