Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 592
  • Last Modified:

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 ??
0
Parth48
Asked:
Parth48
4 Solutions
 
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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
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.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now