Int to Char in C#

C#Casting

C# Problem Overview


What is the best way to convert an Int value to the corresponding Char in Utf16, given that the Int is in the range of valid values?

C# Solutions


Solution 1 - C#

(char)myint;

for example:

Console.WriteLine("(char)122 is {0}", (char)122);

yields:

> (char)122 is z

Solution 2 - C#

int i = 65;
char c = Convert.ToChar(i);

Solution 3 - C#

Although not exactly answering the question as formulated, but if you need or can take the end result as string you can also use

string s = Char.ConvertFromUtf32(56);

which will give you surrogate UTF-16 pairs if needed, protecting you if you are out side of the BMP.

Solution 4 - C#

gimel's answer in PowerShell seems to be:

> [char]65
A
> [char]48
0
> [char]97
a

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBoazView Question on Stackoverflow
Solution 1 - C#gimelView Answer on Stackoverflow
Solution 2 - C#Corey TragerView Answer on Stackoverflow
Solution 3 - C#BoazView Answer on Stackoverflow
Solution 4 - C#JohnL4View Answer on Stackoverflow