Getting ASCII Code in C++

How do I get the ASCII code for a char type variable?

And How do I convert ASCII code back to a character?
You can use a cast to convert it to int from char and the oppoosite.
Here is an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

int main(){
	char ascii;
	int numeric;
	cout << "Give character: ";
	cin >> ascii;
	cout << "Its ascii value is: " << (int) ascii << endl;
	cout << "Give a number to convert to ascii: ";
	cin >> numeric;
	cout << "The ascii value of " << numeric << " is " << (char) numeric;
	return 0;
}
Last edited on
Dear Mitsakos:

Thank you very much for the prompt reply... I will try this...
its just the dec value of ascii code... how 'bout the hex value???
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;
int main()
{
	unsigned char ucCharacter;
	cin >> ucCharacter;
	cout << hex << "0x" << (unsigned int)ucCharacter <<endl;
	int iChNum;
	cin >> hex >> iChNum;
	cout << (unsigned char)iChNum <<endl;
	return 0;
}
Thanks simo110!!! thank you very much!!! mwah!!! XD
Topic archived. No new replies allowed.