C# – How to convert char to int

Converting a char to an int means getting the numeric value that the char represents (i.e. ‘1’ to 1). This is not the same as casting the char to an int, which gives you the char’s underlying value (i.e. ‘1’ is 49).

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

  • Use char.GetNumericValue().
  • Subtract ‘0’ from the char.
  • Use int.Parse() or int.TryParse().

I’ll show examples and discuss these options.

Option 1 – Use char.GetNumericValue()

char.GetNumericValue() converts a char to a double, which you can then cast to an int. Here’s an example of using this method:

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

int two = (int)char.GetNumericValue('2');
Code language: C# (cs)

This converts ‘1’ and ‘2’ to integers 1 and 2.

If the char isn’t a number

char.GetNumericValue() returns -1 if the char can’t be converted to a number. Here’s an example of checking if the number is valid:

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

if (number == -1)
{
    Console.WriteLine($"'{input}' is not a number");
}
Code language: C# (cs)

This outputs:

'a' is not a numberCode language: plaintext (plaintext)

Converting a fraction

char.GetNumericValue() returns a double because it can convert fraction characters (like ‘½’) to their numeric value (0.5). Here’s an example:

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

Console.WriteLine(number); // 0.5
Code language: C# (cs)

Option 2 – Subtract ‘0’ from the char

The fastest way to convert a char to an int is by subtracting ‘0’ from the char. Here’s an example:

int one =  '1' - '0';

int nine = '9' - '0';
Code language: C# (cs)

This works because when you subtract ‘0’ from a char, you’re really subtracting the underlying values. Chars ‘0’ through ‘9’ have the following ASCII decimal values:

ASCII decimal values of '0' through '9'
'0' = 48
'1' = 49
'2' = 50
'3' = 51
'4' = 52
'5' = 53
'6' = 54
'7' = 55
'8' = 56
'9' = 57Code language: plaintext (plaintext)

Therefore, when you do ‘1’ – ‘0’, you’re really doing 49 – 48 = 1. This approach only works because the underlying values are consecutive and in ascending order.

This is the fastest approach, but it’s not very obvious why it works until you see the explanation. To clarify intent in your code, you may want to stick with one of the built-in methods like char.GetNumericValue(). If you do end up using this approach, I suggest wrapping it in a well-named method.

If the char isn’t a number

You can subtract ‘0’ from any char and it’ll give you an integer, even if the char is non-numeric. This can lead to unexpected results. For example, ‘b’ – ‘0’ is 50. To guard against this, check if the input is a number with char.IsDigit() before subtracting ‘0’:

char input = 'b';

if (char.IsDigit(input))
{
    int number = input - '0';

    //use number
}
else
{
    Console.WriteLine($"'{input}' is not a number");
}
Code language: C# (cs)

This outputs:

'b' is not a numberCode language: plaintext (plaintext)

Option 3 – Use int.Parse() or int.TryParse()

You can use int.Parse() to convert a char to an int. The main problem with this approach is it only accepts strings, so you have to convert the char to a string, then the convert the string to an int. Here’s an example:

char input = '1';

int number = int.Parse(input.ToString());
Code language: C# (cs)

Use int.TryParse() to guard against errors

int.Parse() throws a FormatException if the char can’t be converted to an integer. If you want to guard against this, use int.TryParse() instead. This returns false if it couldn’t be converted, which allows you to handle this error however you prefer.

Here’s an example:

char input = 'z';

if (!int.TryParse(input.ToString(), out int number))
{
    Console.WriteLine($"'{input}' is not a number");
}
Code language: C# (cs)

This outputs the following:

'z' is not a numberCode language: plaintext (plaintext)

Performance comparison

I did a simple performance comparison of the three approaches. I ran them against lists of 1k to 100k characters. The Subtract ‘0’ approach is the fastest, followed by char.GetNumericValue(), and way in last place is int.Parse(). Here are the numbers:

input size: 1k
GetNumericValue avg=0.009ms
IntParse avg=0.038ms
Subtract0 avg=0.006ms

input size: 10k
GetNumericValue avg=0.09ms
IntParse avg=0.32ms
Subtract0 avg=0.04ms

input size: 100k
GetNumericValue avg=1.18ms
IntParse avg=2.75ms
Subtract0 avg=0.51msCode language: plaintext (plaintext)

Leave a Comment