Parse string in hexidecmial format to integer in CSharp

Description

The following code shows how to parse string in hexidecmial format to integer.

Example


using System;// w w w  . j a  va  2s .co m
using System.Globalization;

public class Example
{
   public static void Main()
   {
      
      string string2 = "FFFFFF";
      try {
         int number2 = Int32.Parse(string2,System.Globalization.NumberStyles.HexNumber);
         Console.WriteLine(number2);
      }
      catch (OverflowException) {
         Console.WriteLine("'{0}' is out of range of a 32-bit integer.", string2);
      }
      catch (FormatException) {
         Console.WriteLine("The format of '{0}' is invalid.", string2);
      }
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var