Solved

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

Posted on 2011-02-23
573 Views
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
Question by:Parth48
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 39

Accepted Solution

Pratima Pharande earned 125 total points
ID: 34959389
int c = (int)'a';
You can also use the Convert class
int c = Convert.ToInt32('a');
0

LVL 53

Assisted Solution

Dhaest earned 125 total points
ID: 34959392
Can you try the following

int asciiCode = (int)'a';
0

LVL 22

Assisted Solution

Ivo Stoykov earned 125 total points
ID: 34959396
Hi try this

HTH

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

LVL 10

Assisted Solution

Asim Nazir earned 125 total points
ID: 34959409
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

LVL 29

Expert Comment

ID: 34960061
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

LVL 11

Expert Comment

ID: 34960561
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

LVL 39

Expert Comment

ID: 34969437
I have also given the same solution
0

LVL 10

Expert Comment

ID: 34969577
Even my solution was same with details :)
0

Author Comment

ID: 34986059

and give

Dhaest:
Can you try the following

int asciiCode = (int)'a';

full point ...

excellent ...
0

Expert Comment

ID: 35015574
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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that undeâ€¦
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Nobody understands Phishing better than an anti-spam company. Thatâ€™s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With companâ€¦
Starting up a Project
###### Suggested Courses
Course of the Month5 days, 11 hours left to enroll