Convert Char to Int in C#
-
the
char
Data Type inC#
-
Use the
Char.GetNumericValue()
Method to Convertchar
toint
inC#
-
Use the
Convert.ToInt32()
Method to Convertchar
toint
inC#
-
Use the
Int32.Parse()
Method to Convertchar
toint
inC#

C# is an object-oriented programming language. This implies that every variable must be declared while indicating the type of values that variable will store.
C# variables are stored in different forms or types called Data types
.
the char
Data Type in C#
Sample syntax of declaring a variable with Char
data type.
type variableName = value;
char grade = 'A';
char myCharacter = 'X';
char myLuckyNumber = '3';
A single character is stored in the char
data type. The characters must be enclosed in single quotes, such as 'A'
or 'X'
.
Use the Char.GetNumericValue()
Method to Convert char
to int
in C#
The Char.GetNumericValue()
method converts a specified numeric Unicode character to a double-precision floating-point number.
If the method is applied on a char
variable with numeric value enclosed in single quotes, that number is returned, else -1 is returned.
Code Snippet:
using System;
public class GetNumericValue{
public static void Main() {
int result = (int)Char.GetNumericValue('8');
Console.WriteLine(result); // Output: "8"
Console.WriteLine(Char.GetNumericValue('X')); // Output: "-1"
}
}
Output:
8-1
Use the Convert.ToInt32()
Method to Convert char
to int
in C#
The Convert.ToInt32()
function, converts a specified value to a 32-bit signed integer. The ToInt32()
method has so many variants depending on the data type of the argument passed.
Code Snippet:
using System;
public class ConvertCharToInt {
public static void Main() {
char c = '3' ;
Console.WriteLine(Convert.ToInt32(c.ToString())); // Output: "3"
Console.WriteLine(Convert.ToInt32(c)); // Output: "51"
}
}
Output:
351
If the char
data type is passed as an argument into the Convert.Tolnt32()
method, the ASCII
equivalent is returned.
Use the Int32.Parse()
Method to Convert char
to int
in C#
Int32.Parse()
method converts the string
representation of a number to its 32-bit signed integer equivalent. The downside of this method is that a numeric value must be passed as the string
argument.
The char
data type must first be converted to string
data type and must contain a numeric value in single quotes ' '
else the code will throw an overflow exception
.
Code Snippet:
using System;
public class GetNumericValue {
public static void Main() {
char c = '3';
char s = 'x';
string str = c.ToString();
string ex = s.ToString();
Console.WriteLine(Int32.Parse(str));
//Console.WriteLine(Int32.Parse(ex)); // Output: "ThrowEx"
}
}
Output:
3
An exception is thrown if the char
value is not numeric in single quotes ' '
as shown below.
Code Snippet:
using System;
public class GetNumericValue {
public static void Main() {
char s = 'x';
string ex = s.ToString();
Console.WriteLine(Int32.Parse(ex));
}
}
Output:
Unhandled Exception:
System.FormatException: Input string was not in a correct format.
Related Article - Csharp Char
- C# Convert a Char to an Int
- Count Occurrences of a Character Inside a String in C#
- Get the First Character of a String in C#
- Get ASCII Value of Character in C#
- Remove a Character From a String in C#