Converting Integers to Characters in C#

Introduction

In C#, the need to convert integers to their corresponding character representations is a common occurrence. Whether you're working with ASCII values or dealing with character-based operations, understanding the various methods for converting integers to characters is essential for any C# developer.

In this article, we will learn about the different techniques available for this conversion, providing examples and insights into each method's usage.

1. Type Casting

The simplest way to change an integer into a character in C# is by typecasting. We can easily cast an integer to a character type since, at their core, characters are simply numbers (ASCII values). This approach is popular with developers since it is simple and effective.

Example

using System;

class Program
{
    static void Main()
    { 
        int intValue = 65; // Represents 'A' in ASCII
        char charValue = (char)intValue;

        Console.WriteLine("Convert From Int To Char In C# Using Type Casting");
        Console.WriteLine("Integer Output : " + intValue);
        Console.WriteLine("Character Output : " + charValue);
    }
}

Output

Type Casting Output

2. Convert.ToChar()

The Convert.ToChar() method in C# provides an easier and more readable way to convert an integer to its corresponding character representation compared to the direct type casting method. It is part of the `System` namespace and ensures a smooth conversion from an integer to a character.

This method comes in handy when you want to make the conversion more explicit and easy to understand in your code. With Convert.ToChar(), you can quickly and effortlessly convert an integer to its corresponding character without any complexity.

Example

using System;

class Program
{
    static void Main()
    {
        int intValue = 65; // Represents 'A' in ASCII
        char charValue = Convert.ToChar(intValue);

        Console.WriteLine("Convert From Int To Char In C# Using Convert.ToChar()");
        Console.WriteLine("Integer Output : " + intValue);
        Console.WriteLine("Character Output : " + charValue);
    }
}

Output

Output Convert.ToChar()

3. char.ConvertFromUtf32()

The char.ConvertFromUtf32() method in C# is really helpful when you're working with special characters and characters from different languages that go beyond the usual English characters. It allows you to convert a special code (a number that represents a specific character) to its actual character representation.

This method is essential when you need to work with characters that are not part of the standard English alphabet or the common ASCII characters we usually deal with. It's like a magic tool that helps you handle a wider range of characters in your C# programs.

Example

using System;
using System.IO;

class Program
{
    static void Main()
    {
        int intValue1 = 65; // Represents 'A' in ASCII
        int intValue2 = 8364; // Represents the Euro sign '€'
        int intValue3 = 128516; // Represents the smiley face emoji '😄'
        int intValue4 = 960; // Represents the Greek letter 'π' (pi)

        string charAsString1 = char.ConvertFromUtf32(intValue1);
        string charAsString2 = char.ConvertFromUtf32(intValue2);
        string charAsString3 = char.ConvertFromUtf32(intValue3);
        string charAsString4 = char.ConvertFromUtf32(intValue4);

        // Write characters to a file
        File.WriteAllText("unicode_characters.txt",
            $"Character for ASCII 65: {charAsString1}\n" +
            $"Character for Unicode 8364 (Euro sign): {charAsString2}\n" +
            $"Character for Unicode 128516 (Smiley face): {charAsString3}\n" +
            $"Character for Unicode 960 (Greek letter π): {charAsString4}");
    }
}

Output

Some consoles may not support displaying special characters correctly. Therefore, we have written the output to a text file in the example. This allows us to accurately identify how the method works. The following screenshot shows the contents of the text file.

Outpur of char.ConvertToUtf32()

Final Example

In this example, we will see all three methods together and print A to Z and a to z using Type Casting and Convert.ToChar() Method and print Random special character using char.ConvertFromUtf32().

using System;
using System.IO;

class Program
{
    static void Main()
    {
        int CapitalLetterVal = 65;
        int SmallLetterVal = 97;

        Console.WriteLine("Print A to Z and a to z Using Type Casting : ");
        Console.Write("A to Z : ");
        for(int i=1;i<=26;i++, CapitalLetterVal++)
        {
            Console.Write((char)CapitalLetterVal +  " ");
        }
        Console.WriteLine();
        Console.Write("a to z : ");
        for (int i = 1; i <= 26; i++,SmallLetterVal++)
        {
            Console.Write((char)SmallLetterVal + " ");
        }

        CapitalLetterVal = 65;
        SmallLetterVal = 97;

        Console.WriteLine("\n\n");
        Console.WriteLine("Print A to Z and a to z Using Convert.ToChar() : ");
        Console.Write("A to Z : ");
        for (int i = 1; i <= 26; i++, CapitalLetterVal++)
        {
            Console.Write(Convert.ToChar(CapitalLetterVal) + " ");
        }
        Console.WriteLine();
        Console.Write("a to z : ");
        for (int i = 1; i <= 26; i++, SmallLetterVal++)
        {
            Console.Write(Convert.ToChar(SmallLetterVal) + " ");
        }


        Console.WriteLine("\n\n");
        Console.WriteLine("Print Random Numbers In TextFile Using char.ConvertFromUtf32() : ");
        Random random = new Random();
        String randomCharacters = "";
        for (int i = 1; i <= 26; i++)
        {
            int utf32Value = random.Next(225, 10000);
            string specialCharacter = char.ConvertFromUtf32(utf32Value);
            randomCharacters = randomCharacters + " " + specialCharacter + "  ";
        }
        // Write characters to a file
        File.WriteAllText("unicode_characters.txt",randomCharacters);
    }
}

Output

Output of A To Z And a to z

Outpur of special character

Summary

Converting integers to characters in C# is a basic task, and we have different methods to do it. Each method has its own benefits, and the choice depends on what you need for your code to be clear and easy to read. Type casting is short and straightforward. Convert.ToChar() makes the conversion explicit, and char.ConvertFromUtf32() handles special characters.


Similar Articles