Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 22-04-2009
Member
 
Join Date: Apr 2009
Posts: 3
Converting Char value to ASCII number, C++

Hey guys,

i know there is already a post like this up, and i'v read it but it's not really helping me.
I need to convert a char value into it's ASCII number equivilent.
The letter is an input from the user so it's completly random, and it's just a single letter.
I know how to write the program so that it just outputs the value as a number but what i actually need to do is convert it and then assign that number to a different variable.
I'm assuming there is a particular function i can use, i just don't know what it is!
If worst comes to worst i suposse i could just write an if statment for every letter of the alphabet but i'm hoping there is an easier way XD!
Reply With Quote
  #2  
Old 22-04-2009
Member
 
Join Date: Mar 2008
Posts: 198
Re: Coverting Char value to ASCII number, C++

Refer to this thread: How to convert a character to ASCII in C++?

Hope this will help. Good luck
Reply With Quote
  #3  
Old 22-04-2009
Member
 
Join Date: Oct 2008
Posts: 167
Re: Coverting Char value to ASCII number, C++

It's simple. A character that you're using in your program already have an ASCII-value, but you sees it like a character. If you want to have the value itself, you can simply convert the character to an integer.

Code:
// C, with C type-casting
char cMyCharacter = 'A';
int iMyAsciiValue = (int)cMyCharacter;

// C++, with C++ type-casting
char cMyCharacter = 'A';
int iMyAsciiValue = static_cast<int>(cMyCharacter);
You can actually leave out the type-casting part. C/C++ is built in the way that when an integer-variable meets a char-variable, it automatic converts.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Converting Char value to ASCII number, C++"
Thread Thread Starter Forum Replies Last Post
Converting the floating point number to the hexadecimal on the Microsoft Excel 2010 Aag-Man Windows Software 5 11-07-2011 10:38 AM
Converting a string number into sequence of digits KAIRU26 Software Development 5 13-03-2010 04:46 PM
Converting Char to Int in PHP GlassFish Software Development 4 05-03-2010 08:06 PM
Converting string to char Abdullah30 Software Development 3 03-09-2009 11:09 PM
How to Create ASCII Art avvia Windows Software 3 29-06-2009 11:06 AM


All times are GMT +5.5. The time now is 12:23 AM.