Uri.IsHexDigit() Method in C#

The Uri.IsHexDigit() method in C# determines whether a specified character is a valid hexadecimal digit. This method returns true if the character is a valid hexadecimal digit (0-9, A-F, a-f), and false otherwise.

This method is commonly used when working with URL encoding, parsing hexadecimal values, or validating user input that should contain only hexadecimal characters.

Syntax

Following is the syntax −

public static bool IsHexDigit(char ch);

Parameters

  • ch − The character to validate as a hexadecimal digit.

Return Value

Returns true if the character is a valid hexadecimal digit (0-9, A-F, a-f); otherwise, false.

Valid Hexadecimal Characters Digits 0-9 Uppercase A-F Lowercase a-f Invalid Characters # $ @ % & * etc. Total valid hex characters: 22 (0-9, A-F, a-f)

Using IsHexDigit() with Valid Characters

The following example demonstrates checking various valid hexadecimal characters −

using System;

public class Demo {
   public static void Main() {
      char ch = 'e';
      Console.WriteLine("Character value = " + ch);
      
      int res = Uri.FromHex(ch);
      Console.WriteLine("Converted int value = " + res);
      
      if (Uri.IsHexDigit(ch)) {
         Console.WriteLine("Valid hexadecimal digit");
      } else {
         Console.WriteLine("Invalid hexadecimal digit");
      }
   }
}

The output of the above code is −

Character value = e
Converted int value = 14
Valid hexadecimal digit

Using IsHexDigit() with Invalid Characters

The following example shows how the method handles invalid hexadecimal characters −

using System;

public class Demo {
   public static void Main() {
      char ch = '#';
      Console.WriteLine("Character value = " + ch);
      
      if (Uri.IsHexDigit(ch)) {
         Console.WriteLine("Valid hexadecimal digit");
      } else {
         Console.WriteLine("Invalid hexadecimal digit");
      }
   }
}

The output of the above code is −

Character value = #
Invalid hexadecimal digit

Testing Multiple Characters

Here's an example that tests multiple characters to demonstrate the range of valid hexadecimal digits −

using System;

public class Demo {
   public static void Main() {
      char[] testChars = {'0', '9', 'A', 'F', 'a', 'f', 'G', 'z', '@'};
      
      foreach (char ch in testChars) {
         bool isHex = Uri.IsHexDigit(ch);
         Console.WriteLine($"'{ch}' is {(isHex ? "valid" : "invalid")} hex digit");
      }
   }
}

The output of the above code is −

'0' is valid hex digit
'9' is valid hex digit
'A' is valid hex digit
'F' is valid hex digit
'a' is valid hex digit
'f' is valid hex digit
'G' is invalid hex digit
'z' is invalid hex digit
'@' is invalid hex digit

Conclusion

The Uri.IsHexDigit() method provides a simple way to validate whether a character is a valid hexadecimal digit (0-9, A-F, a-f). This method is particularly useful when working with URL encoding, parsing hexadecimal strings, or validating user input that should contain only hexadecimal characters.

Updated on: 2026-03-17T07:04:36+05:30

167 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements