Microsoft KB Archive/835454

= 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 ID: 835454

Article Last Modified on 5/17/2007

-

APPLIES TO


 * 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 Studio .NET 2002 Professional Edition
 * Microsoft Visual Studio .NET 2002 Enterprise Architect
 * Microsoft Visual Studio .NET 2002 Enterprise Developer
 * Microsoft Visual Studio .NET 2002 Academic Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-





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 &quot;0x&quot; to specify a hexadecimal format to the Int32.Parse method, you may receive a &quot;System.FormatException&quot; error message.''



IN THIS TASK

 * INTRODUCTION
 * Requirements
 * The Int32.Parse method
 * The NumberStyles enumeration
 * Step-by-step sample
 * Troubleshooting
 * REFERENCES



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
 Start Visual Studio .NET. On the File menu, point to New, and then click Project. The New Project dialog box appears. Under Project Types, click Visual C# Projects. Under Templates, click Windows Application. Click OK. By default, a file that is named Form1.cs is created. Right-click the Form1 Windows Form, and then click View Code.</li>  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 description for Form1. ///   public class Form1 : System.Windows.Forms.Form {     ///       /// Required designer variable. ///      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(&quot;000000&quot;); }

///      /// Clean up any resources that were being used. ///      protected override void Dispose( bool disposing ) {        if( disposing ) {           if (components != null) {              components.Dispose; }        }         base.Dispose( disposing ); }

#region Windows Form Designer generated code ///      /// Required method for Designer support - do not modify /// the contents of this method by using the code editor. ///      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 = &quot;Form1&quot;; this.Text = &quot;Form1&quot;; }     #endregion

///      /// The main entry point for the application. ///      [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); }  } } </li> On the Build menu, click Build Solution.</li> On the Debug menu, click Start to run the application. The Form1 Windows Form appears. The background color of the form is black.</li></ol>

back to the top

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

back to the top

<div class="references_section">