C# | Uri.FromHex() Method
Last Updated :
30 Apr, 2019
Uri.FromHex(Char) Method is used to get the decimal value of a hexadecimal digit.
Syntax: public static int FromHex (char digit);
Here, it takes the hexadecimal digit (0-9, a-f, A-F) to convert.
Return Value: This method returns an Int32 value that contains a number from 0 to 15 that corresponds to the specified hexadecimal digit.
Exception: This method throws ArgumentException if the digit is not a valid hexadecimal digit (0-9, a-f, A-F).
Below programs illustrate the use of Uri.FromHex(Char) Method:
Example 1:
using System;
using System.Globalization;
class GFG {
public static void Main()
{
char value = 'A' ;
int dec = Uri.FromHex(value);
Console.WriteLine( "Converted int value : {0}" , dec);
}
}
|
Output:
Converted int value : 10
Example 2: For ArgumentException
using System;
using System.Globalization;
class GFG {
public static void Main()
{
try {
char value = '.' ;
int dec = Uri.FromHex(value);
Console.WriteLine( "Converted int value : {0}" , dec);
}
catch (ArgumentException e)
{
Console.WriteLine( "Digit should be a valid " +
"Hexadecimal digit (0-9, a-f, A-F)." );
Console.Write( "Exception Thrown: " );
Console.Write( "{0}" , e.GetType(), e.Message);
}
}
}
|
Output:
Digit should be a valid Hexadecimal digit (0-9, a-f, A-F).
Exception Thrown: System.ArgumentException
Reference:
Share your thoughts in the comments
Please Login to comment...