C# | Uri.FromHex() Method
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:
// C# program to demonstrate the // Uri.FromHex(Char) Method using System; using System.Globalization; class GFG { // Main Method public static void Main() { // Declaring and initializing Char value char value = 'A'; // Gets the decimal value // of a hexadecimal digit. // using FromHex() method int dec = Uri.FromHex(value); // Displaying the result Console.WriteLine("Converted int value : {0}", dec); } } |
Converted int value : 10
Example 2: For ArgumentException
// C# program to demonstrate the // Uri.FromHex(Char) Method using System; using System.Globalization; class GFG { // Main Method public static void Main() { try { // Declaring and initializing // Char value char value = '.'; // Gets the decimal value // of a hexadecimal digit. // using FromHex() method int dec = Uri.FromHex(value); // Displaying the result 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); } } } |
Digit should be a valid Hexadecimal digit (0-9, a-f, A-F). Exception Thrown: System.ArgumentException
Reference:
Recommended Posts:
- Difference between Method Overriding and Method Hiding in C#
- C# | String.Contains() Method
- C# | Uri.IsHexDigit() Method
- C# | Uri.IsBaseOf(Uri) Method
- C# | Method Overriding
- C# | Math.Log() Method
- C# | Dictionary.Add() Method
- C# | Trim() Method
- C# | Math.Tan() Method
- C# | Math.Cos() Method
- C# | IndexOfAny() Method
- C# | CopyTo() Method
- C# | Math.Sin() Method
- C# | Math.Abs() Method | Set - 2
- C# | Math.Abs() Method | Set - 1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



