how can i convert character to ascii code and storing it in integer in C# .net window application??

i want to convert character to ascii code and storing it into integer , but how can i do this ??

my coding is below ..

int i = Convert.ToInt16('a');

but i got error in it ??

what can i do ??
Parth48Asked:
Who is Participating?
 
Pratima PharandeCommented:
int c = (int)'a';
You can also use the Convert class
int c = Convert.ToInt32('a');
0
 
DhaestCommented:
Can you try the following

int asciiCode = (int)'a';
0
 
Ivo StoykovCommented:
Hi try this

HTH

Ivo Stoykov
int i = (int) 'a';

Open in new window

0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
Asim NazirCommented:
THis is how it's:
int i = (int)'a';
Just cast the char to an integer and you have it (assuming the character actually comes from the ASCII set of characters).

Asim
0
 
Göran AnderssonCommented:
If you mean that you want the character code in Unicode (not ASCII), then a character translates into an integer without the need for conversion or explicit casting:

  int i = 'a';

If you really want the ASCII code, you use the ASCII encoding:

  int i = Encoding.ASCII.GetBytes("a")[0];
0
 
Sudhakar PulivarthiProject Lead - EngineeringCommented:
Hi,

There is no error in your code in this statement. It works perfect !!

int i = Convert.ToInt16('a');
The result in i is 97 after this statement.

Please check the error is doe to some other statement.
0
 
Pratima PharandeCommented:
I have also given the same solution
0
 
Asim NazirCommented:
Even my solution was same with details :)
0
 
Parth48Author Commented:
please close this question ...

and give

Dhaest:
Can you try the following

int asciiCode = (int)'a';

full point ...

excellent ...
0
 
South ModModeratorCommented:
All,
 
Following an 'Objection' by pratima_mcs (at http://www.experts-exchange.com/Q_26844572.html) to the intended closure of this question, it has been reviewed by at least one Moderator and is being closed as recommended by the Expert.
 
At this point I am going to re-start the auto-close procedure.
 
Thank you,
 
SouthMod
Community Support Moderator
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.