Try to Parse string in Hexidecimal format to int in CSharp

Description

The following code shows how to try to Parse string in Hexidecimal format to int.

Example


using System;/*from  w w  w  .ja  v a2  s.  c  om*/
using System.Globalization;

public class StringParsing
{
   public static void Main()
   {
      string numericString;
      NumberStyles styles;

      numericString = "0x80C1";
      CallTryParse(numericString, NumberStyles.HexNumber);      

   }

   private static void CallTryParse(string stringToConvert, NumberStyles styles)
   {
      int number;
      CultureInfo provider = CultureInfo.InvariantCulture;

      bool result = Int32.TryParse(stringToConvert, styles, provider, out number);
      if (result)
         Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, number);
      else
         Console.WriteLine("Attempted conversion of '{0}' failed.", 
                           Convert.ToString(stringToConvert));
   }
}




















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