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 (835454)



The information in this article applies to:

  • Microsoft Visual Studio .NET (2003), Professional Edition
  • Microsoft Visual Studio .NET (2003), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2003), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2003), Academic Edition
  • Microsoft Visual Studio .NET (2002), Professional Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2002), Academic Edition
  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual C# .NET (2002)

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.

back to the top

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 2003, Microsoft Windows XP, or Microsoft Windows 2000
  • Microsoft Visual Studio .NET 2003 or Microsoft Visual Studio .NET 2002
back to the top

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.

back to the top

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.

back to the top

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);
          }
       }
    }
  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.
back to the top

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.

back to the top

Modification Type:MinorLast Reviewed:8/16/2005
Keywords:kbvs2002sp1sweep kbForms kbColor kbUser kbAppDev kbcode kbBug kbHOWTOmaster KB835454 kbAudDeveloper kbAudEndUser