Link to home
Start Free TrialLog in
Avatar of fpoyavo
fpoyavoFlag for United States of America

asked on

simple one

Hi Experts,

Is there ready class in C# so that I can get a number instead of letter.
Example:     I pass "a" and get "1"
                     pass "b" and get "2"
                     pass "c" and get "3" ....etc

Thank you.
Avatar of vadivhere
vadivhere

I dont think so, but this is very easy to make one.

Class CharToNumber
{
      public CharToNumber
      {}
   
      public static int getInt(char charValue)
     {
                switch (charValue)
                  {
                           case 'a':
                                   return 1;
                                   break;
                           case 'b':
                                   return 2;
                                   break;
                           ......
                           default:
                                  break;
                   }
    }

you can call this without declaring an object
 
                    main()
                    {
                             int x = CharToNumber.getInt('a');
                     }

Hope, this helps you

Cheers
Vadivel Kumar

         

double d = char.GetNumericValue('1');

Cheers
Jatinder
Hi,

You can use this method:

private int CharToInt(char ch)
{
      return (int)ch - (int)'a' + 1;
}

Gena
ASKER CERTIFIED SOLUTION
Avatar of sun4sunday
sun4sunday
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial