Convert the value of the specified string to its equivalent Unicode character in C#

To convert the value of a specified string to its equivalent Unicode character in C#, you can use the Char.TryParse method. This method attempts to convert a string representation to a single Unicode character and returns a boolean indicating whether the conversion was successful.

Syntax

Following is the syntax for Char.TryParse method −

public static bool TryParse(string s, out char result)

Parameters

  • s − The string to convert. Must be exactly one character long for successful conversion.

  • result − When the method returns, contains the Unicode character equivalent if conversion succeeded, or the null character ('\0') if it failed.

Return Value

Returns true if the string was successfully converted to a character; otherwise, false.

Using Char.TryParse with Multi-Character String

When the input string contains multiple characters, the conversion fails −

using System;

public class Demo {
   public static void Main() {
      bool res;
      char ch;
      res = Char.TryParse("10", out ch);
      Console.WriteLine("Conversion successful: " + res);
      Console.WriteLine("Character value: '" + ch + "'");
      Console.WriteLine("Unicode value: " + (int)ch);
   }
}

The output of the above code is −

Conversion successful: False
Character value: ''
Unicode value: 0

Using Char.TryParse with Single Character

When the input string contains exactly one character, the conversion succeeds −

using System;

public class Demo {
   public static void Main() {
      bool res;
      char ch;
      res = Char.TryParse("P", out ch);
      Console.WriteLine("Conversion successful: " + res);
      Console.WriteLine("Character value: '" + ch + "'");
      Console.WriteLine("Unicode value: " + (int)ch);
   }
}

The output of the above code is −

Conversion successful: True
Character value: 'P'
Unicode value: 80

Converting Unicode Code Points to Characters

To convert Unicode code points directly to characters, use Convert.ToChar or casting −

using System;

public class Demo {
   public static void Main() {
      // Convert Unicode code point to character
      char ch1 = Convert.ToChar(65);  // ASCII 'A'
      char ch2 = (char)8364;          // Euro symbol
      char ch3 = '\u0041';            // Unicode escape for 'A'
      
      Console.WriteLine("ASCII 65: " + ch1);
      Console.WriteLine("Unicode 8364: " + ch2);
      Console.WriteLine("Unicode escape \u0041: " + ch3);
   }
}

The output of the above code is −

ASCII 65: A
Unicode 8364: ?
Unicode escape \u0041: A

Conclusion

The Char.TryParse method safely converts single-character strings to Unicode characters, returning false for invalid inputs. For direct Unicode code point conversion, use Convert.ToChar, casting, or Unicode escape sequences like \u0041.

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

442 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements