How to convert char to int?

C#.Net

C# Problem Overview


What is the proper way to convert a char to int? This gives 49:

int val = Convert.ToInt32('1');
//int val = Int32.Parse("1"); // Works

I don't want to convert to string and then parse it.

C# Solutions


Solution 1 - C#

I'm surprised nobody has mentioned the static method built right into System.Char...

int val = (int)Char.GetNumericValue('8');
// val == 8

Solution 2 - C#

how about (for char c)

int i = (int)(c - '0');

which does substraction of the char value?

Re the API question (comments), perhaps an extension method?

public static class CharExtensions {
    public static int ParseInt32(this char value) {
        int i = (int)(value - '0');
        if (i < 0 || i > 9) throw new ArgumentOutOfRangeException("value");
        return i;
    }
}

then use int x = c.ParseInt32();

Solution 3 - C#

What everyone is forgeting is explaining WHY this happens.

A Char, is basically an integer, but with a pointer in the ASCII table. All characters have a corresponding integer value as you can clearly see when trying to parse it.

Pranay has clearly a different character set, thats why HIS code doesnt work. the only way is

int val = '1' - '0';

because this looks up the integer value in the table of '0' which is then the 'base value' subtracting your number in char format from this will give you the original number.

Solution 4 - C#

int i = (int)char.GetNumericValue(c);

Yet another option:

int i = c & 0x0f;

This should accomplish this as well.

Solution 5 - C#

int val = '1' - '0';

This can be done using ascii codes where '0' is the lowest and the number characters count up from there

Solution 6 - C#

int val = '1' - 48;

Solution 7 - C#

You may use the following extension method:

public static class CharExtensions
    {
        public static int CharToInt(this char c)
        {
            if (c < '0' || c > '9')
                throw new ArgumentException("The character should be a number", "c");
        
            return c - '0';
        }
    }

Solution 8 - C#

The most secure way to accomplish this is using Int32.TryParse method. See here: http://dotnetperls.com/int-tryparse

Solution 9 - C#

int val = '1' & 15;

The binary of the ASCII charecters 0-9 is:

0   -   00110000

1   -   00110001

2   -   00110010

3   -   00110011

4   -   00110100

5   -   00110101

6   -   00110110

7   -   00110111

8   -   00111000

9   -   00111001

and if you take in each one of them the first 4 LSB(using bitwise AND with 8'b00001111 that equels to 15) you get the actual number (0000 = 0,0001=1,0010=2,... )

Solution 10 - C#

An extension of some other answers that covers hexadecimal representation:

public int CharToInt(char c) 
{
    if (c >= '0' && c <= '9') 
    {
        return c - '0';
    }
    else if (c >= 'a' && c <= 'f') 
    {
        return 10 + c - 'a';
    }
    else if (c >= 'A' && c <= 'F') 
    {
        return 10 + c - 'A';
    }
             
    return -1;
}

Solution 11 - C#

You can try something like this:

int val = Convert.ToInt32("" + '1');

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontvrView Question on Stackoverflow
Solution 1 - C#Joel MuellerView Answer on Stackoverflow
Solution 2 - C#Marc GravellView Answer on Stackoverflow
Solution 3 - C#StefanvdsView Answer on Stackoverflow
Solution 4 - C#GrifView Answer on Stackoverflow
Solution 5 - C#TristanView Answer on Stackoverflow
Solution 6 - C#x2.View Answer on Stackoverflow
Solution 7 - C#Eugene CheverdaView Answer on Stackoverflow
Solution 8 - C#MUG4NView Answer on Stackoverflow
Solution 9 - C#Tomer WolbergView Answer on Stackoverflow
Solution 10 - C#OlekView Answer on Stackoverflow
Solution 11 - C#bribaView Answer on Stackoverflow