This post will discuss how to convert a char to int in C#.

1. Using Char.GetNumericValue() method

The recommended approach is to use the in-built GetNumericValue() method to convert a numeric Unicode character to its numeric equivalent.

The following example demonstrates the working of the GetNumericValue() method. It expects a char representation of a numeric value and returns a double value. A cast is needed to convert the double value to an int.

Download  Run Code

2. Difference with ‘0’

We know that each of the ASCII characters is represented by values from 0 through 127. To get the integer equivalent of characters ‘0’ to ‘9’, simply subtract ‘0’ from it. The following code example shows how to implement this:

Download  Run Code

3. Using CharUnicodeInfo.GetDecimalDigitValue() method

Another approach is to use the CharUnicodeInfo.GetDecimalDigitValue() method, which returns the decimal digit value of the specified numeric Unicode character. This method is demonstrated below:

Download  Run Code

4. Using Int32.Parse() method

The following code example demonstrates how to use Int32.Parse() and Int32.TryParse() to convert char to its integer equivalent.

Download  Run Code

That’s all about converting char to int in C#.