Convert Char to Int in C#

Abdullahi Salawudeen Jan 30, 2023 Feb 17, 2022
  1. the char Data Type in C#
  2. Use the Char.GetNumericValue() Method to Convert char to int in C#
  3. Use the Convert.ToInt32() Method to Convert char to int in C#
  4. Use the Int32.Parse() Method to Convert char to int in C#
Convert Char to Int in C#

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.
Abdullahi Salawudeen avatar Abdullahi Salawudeen avatar

Abdullahi is a full-stack developer and technical writer with over 5 years of experience designing and implementing enterprise applications. He loves taking on new challenges and believes conceptual programming theories should be implemented in reality.

LinkedIn GitHub

Related Article - Csharp Char

Related Article - Csharp Int