HOWTO · Csharp

C# Convert Int to Char

This tutorial teaches how to cast int to char in C#.

On this page

In C#, choose the conversion based on what the integer represents: cast a value in the char range, use Convert.ToChar() for a checked conversion, use Char.ConvertFromUtf32() for a Unicode code point, or use ToString()[0] to read the first decimal digit of a nonnegative integer. These operations return different results, so digit extraction is not a general integer-to-character conversion. The examples below are unexecuted here; their code is provided to show each operation.

A C# char stores one UTF-16 code unit. It can hold values from 0 through 65535, but some Unicode characters need two code units. If your input is a Unicode code point rather than a value meant to fit in one char, use the method that returns a string.

Cast an int to char in C# with an explicit cast

An explicit cast converts an integer value to a char. For a value such as 102, the corresponding UTF-16 code unit represents f. A char is one UTF-16 code unit, so this cast does not turn every Unicode code point into a complete character. Supplementary code points need two code units and cannot fit in one char. In a checked context, an out-of-range numeric conversion is checked; in an unchecked context, the conversion keeps the low 16 bits. Keep the input in the char range when you want a direct mapping.

using System;

public class Program
{
    public static void Main()
    {
        int value = 102;
        char character = (char)value;
        Console.WriteLine($"(char){value} is {character}");
    }
}

Convert an int to char in C# with Convert.ToChar()

Convert.ToChar() converts an integer in the char range, from 0 through 65535, to a char. For an int outside that range, it throws OverflowException. Choose this method when you want the range checked rather than relying on an explicit cast. The returned value is still a single UTF-16 code unit, not a complete representation for every Unicode code point.

using System;

public class Program
{
    public static void Main()
    {
        int value = 65;
        char character = Convert.ToChar(value);
        Console.WriteLine($"Convert.ToChar({value}) is {character}");
    }
}

Convert a Unicode code point in C# with Char.ConvertFromUtf32()

When an integer represents a Unicode code point, Char.ConvertFromUtf32() returns a string containing its UTF-16 representation. It accepts valid Unicode scalar values: values in the surrogate range and values above U+10FFFF are not valid inputs. A code point in the supplementary range is represented by two char values, so the returned string—not just its first element—is the complete character representation. This is the suitable choice when the integer identifies a Unicode code point and you need its text representation.

using System;

public class Program
{
    public static void Main()
    {
        int codePoint = 0x1F600;
        string character = Char.ConvertFromUtf32(codePoint);
        Console.WriteLine(character);
    }
}

Get the first decimal digit of an int in C# with ToString()

value.ToString()[0] gets the first character in the integer’s decimal text; it does not convert the numeric value to the character represented by that value. For a nonnegative value such as 80, that character is the digit 8. For a negative value, the first character is the sign, so this expression does not return a digit. Use it only when the goal is to inspect the first written digit of a known nonnegative integer. If you need to process every digit, keep the value as a string or use numeric operations suited to that task.

using System;

public class Program
{
    public static void Main()
    {
        int value = 80;
        char firstCharacter = value.ToString()[0];
        Console.WriteLine($"value.ToString()[0] is {firstCharacter}");
    }
}

Choose the right int-to-char conversion in C#

  • Use an explicit cast when the integer is a value intended to map directly to one char code unit.
  • Use Convert.ToChar() when the value should be checked against the char range.
  • Use Char.ConvertFromUtf32() when the integer is a valid Unicode code point and the result may need multiple UTF-16 code units; keep the returned string intact.
  • Use ToString()[0] only to obtain the first decimal digit character from a nonnegative integer.

These methods solve different tasks: a cast and Convert.ToChar() return one char, Char.ConvertFromUtf32() returns a string for a Unicode code point, and ToString()[0] reads a character from formatted decimal text.