Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Convert.ToChar Method in C#
The Convert.ToChar() method in C# converts a specified value to its equivalent Unicode character representation. This method accepts various data types including integers, bytes, strings, and other numeric types, and returns a char value.
Syntax
Following are the common overloads of the Convert.ToChar() method −
public static char ToChar(byte value) public static char ToChar(int value) public static char ToChar(string value) public static char ToChar(object value)
Parameters
-
value − The value to be converted to a Unicode character. This can be a numeric type, string, or object.
Return Value
Returns a Unicode character that is equivalent to the specified value.
Using Convert.ToChar() with Numeric Values
Example
using System;
public class Demo {
public static void Main() {
sbyte[] byteVal = { 92, 111, 115, 104 };
char charVal;
Console.WriteLine("Converting sbyte values to characters:");
foreach (sbyte b in byteVal) {
charVal = Convert.ToChar(b);
Console.WriteLine("{0} converted to '{1}'", b, charVal);
}
}
}
The output of the above code is −
Converting sbyte values to characters: 92 converted to '' 111 converted to 'o' 115 converted to 's' 104 converted to 'h'
Using Convert.ToChar() with Different Data Types
Example
using System;
public class Program {
public static void Main() {
// Converting int to char
int intVal = 65;
char charFromInt = Convert.ToChar(intVal);
Console.WriteLine("Int {0} to char: '{1}'", intVal, charFromInt);
// Converting string to char
string strVal = "B";
char charFromStr = Convert.ToChar(strVal);
Console.WriteLine("String "{0}" to char: '{1}'", strVal, charFromStr);
// Converting byte to char
byte byteVal = 67;
char charFromByte = Convert.ToChar(byteVal);
Console.WriteLine("Byte {0} to char: '{1}'", byteVal, charFromByte);
// Converting ushort to char
ushort ushortVal = 68;
char charFromUshort = Convert.ToChar(ushortVal);
Console.WriteLine("Ushort {0} to char: '{1}'", ushortVal, charFromUshort);
}
}
The output of the above code is −
Int 65 to char: 'A' String "B" to char: 'B' Byte 67 to char: 'C' Ushort 68 to char: 'D'
Converting ASCII Values to Characters
Example
using System;
public class ASCIIConverter {
public static void Main() {
int[] asciiValues = { 72, 101, 108, 108, 111 };
Console.Write("ASCII values ");
foreach (int ascii in asciiValues) {
Console.Write(ascii + " ");
}
Console.Write("convert to: ");
foreach (int ascii in asciiValues) {
char character = Convert.ToChar(ascii);
Console.Write(character);
}
Console.WriteLine();
}
}
The output of the above code is −
ASCII values 72 101 108 108 111 convert to: Hello
Important Considerations
-
The method throws OverflowException if the value is outside the range of a
char(0 to 65535). -
When converting a string, it must contain exactly one character, otherwise ArgumentException is thrown.
-
Negative values cannot be converted to
charand will throw an OverflowException.
Conclusion
The Convert.ToChar() method provides a convenient way to convert numeric values and single-character strings to Unicode characters. It's particularly useful when working with ASCII values or when you need to convert between different numeric types and character representations in C#.
