We can use the HexEscape()
method of the Uri class to get the hexadecimal equivalent of a character in C#. This method returns a string that is the hexadecimal equivalent of the given character.
Uri.HexEscape(character)
character
: This is the character whose hexadecimal equivalent we want to obtain.
The HexEscape()
method returns a string, which is the hexadecimal equivalent of the specified character.
using System;class HelloWorld{static void Main(){// create and initialize some characterschar c1 = 'A';char c2 = 'C';char c3 = 'e';char c4 = 'a';// print hexadecimal equivalents of charactersConsole.WriteLine(Uri.HexEscape(c1));Console.WriteLine(Uri.HexEscape(c2));Console.WriteLine(Uri.HexEscape(c3));Console.WriteLine(Uri.HexEscape(c4));}}
HexEscape()
method of the Uri class. Then, we print the results to the console.Note: When we specify a character that is greater than 255, we get the
ArgumentOutOfRangeException
error. This error is thrown when the range of values that are acceptable for a particular method is exceeded.