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!
Re: Coverting Char value to ASCII number, C++
Refer to this thread: http://forums.techarena.in/software-...nt/1125162.htm
Hope this will help. Good luck
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.