Alex Rivera | Logout

How to convert char to int?

Asked 2010-09-08T08:13:26.530
131

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.

Edit
Report

2 Answers

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

Yet another option:

int i = c & 0x0f;

This should accomplish this as well.

answered 2010-09-08T09:00:11.037
4
int val = '1' - '0';

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

answered 2010-09-08T08:16:19.443

Your Answer