How to convert char to int in C#

By FoxLearn 2/4/2025 6:20:48 AM   61
onverting a char to an int means obtaining the numeric value that the char represents (e.g., ‘1’ becomes 1). This is different from casting a char to an int, which gives you the char's ASCII or Unicode value (e.g., ‘1’ is 49 in Unicode).

There are several ways to convert a char to an int:

Use char.GetNumericValue()

The char.GetNumericValue() method converts a char into a double, which you can then cast to an int.

int one = (int)char.GetNumericValue('1');
int three = (int)char.GetNumericValue('3');

This converts '1' and '3' into integers 1 and 3 respectively.

If the char cannot be converted to a number, char.GetNumericValue() will return -1.

char input = 'z';
var number = char.GetNumericValue(input);

if (number == -1)
{
    Console.WriteLine($"'{input}' is not a number");
}

This will output:

'z' is not a number

The method also works for fraction characters, returning their numeric value as a double.

char input = '¼';
var number = Char.GetNumericValue(input);

Console.WriteLine(number); // 0.25

Subtract '0' from the char

One of the fastest ways to convert a char to an int is by subtracting '0' from it.

int one = '1' - '0';
int eight = '8' - '0';

This works because each digit from '0' to '9' has a consecutive ASCII value (from 48 to 57). When you subtract '0', you are essentially subtracting the ASCII value of '0' (48), which results in the digit’s numeric value.

For example:

'1' has an ASCII value of 49, and '0' has 48, so '1' - '0' = 49 - 48 = 1.

While this is the most efficient method, it’s not as clear what’s happening behind the scenes. For non-numeric characters, you may get unexpected results. For example, 'a' - '0' would result in 49.

To avoid errors, it’s best to check whether the character is a digit before performing the subtraction:

char input = 'a';

if (char.IsDigit(input))
{
    int number = input - '0';
    // Use number
}
else
{
    Console.WriteLine($"'{input}' is not a number");
}

This outputs:

'a' is not a number

Use int.Parse() or int.TryParse()

You can also use int.Parse() to convert a char to an int, though it requires converting the char to a string first.

char input = '4';
int number = int.Parse(input.ToString());

This converts the char '4' to the integer 4.

For example, Using int.TryParse() to handle errors:

int.Parse() throws an exception if it can't convert the string. To safely handle this, you can use int.TryParse(), which returns a boolean indicating success or failure:

char input = 'x';

if (!int.TryParse(input.ToString(), out int number))
{
    Console.WriteLine($"'{input}' is not a number");
}

This will output:

'x' is not a number

Performance Comparison

A quick performance test on three approaches shows that subtracting '0' is the fastest method, followed by char.GetNumericValue(), and int.Parse() is the slowest. Here's a summary:

  • Input size: 1k

    • GetNumericValue avg = 0.009ms
    • IntParse avg = 0.038ms
    • Subtract '0' avg = 0.006ms
  • Input size: 10k

    • GetNumericValue avg = 0.09ms
    • IntParse avg = 0.32ms
    • Subtract '0' avg = 0.04ms
  • Input size: 100k

    • GetNumericValue avg = 1.18ms
    • IntParse avg = 2.75ms
    • Subtract '0' avg = 0.51ms

In summary, all methods are useful depending on your needs, but Subtract '0' offers the best performance. However, if clarity is your priority, using char.GetNumericValue() or int.TryParse() is recommended.