Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 588
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 PulivarthiCommented:
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

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

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