ASCII value of a character

# include<stdio.h>

int main()
 char ch = 'A';
 printf("ASCII value of %c is %d \n", ch,ch);


Do I have an equivalent code in c++?????
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

#include <iostream>
using namespace std;

int main()
  char ch = 'A';
  cout<<"ASCII value of "<<ch<<" is "<<(int)ch<<endl;
  return 1;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Yes, you have several ways:

void do_something()
   char ch = 'A';

   // use #include <stdio.h> or #include <cstdio>
   // for this to work.
   printf( "ASCII value of %c is %d\n", ch, ch);

   // use #include <iostream> for this to work.
   // and also use 'using namespace std;' if you want
   // to avoid the std:: in front.
   std::cout << "ASCII value of " << ch << " is "
       << int(ch) << std::endl;

There are actually several more ways but the above are both standard and work on any platform and should probably be enough for most purposes.

Watch out for non-printable and special ascii characters, printing out a ch if ch = '\n' etc gives a result but not necessarily one you would easily notice.

Also, be aware that ASCII is more or less out per se. Unicode is in. On the other hand, the first 128 codes of unicode is identical to ASCII so it's not quite out after all.

Check out if you want to know more about unicode and you can check out some RFCs if you wnat ot know about UTF-8 or UTF-16 or UTF-32.

There is a function toascii() defined in ctype.h u can aslo use that to get the ASCII of any char.

Be aware that toascii doesn't do exactly what sarda_ramesh appear to expect.

The function forces the char value to ascii by clearing the high bit. This means that a value such as:

  0xe1  becomes 0x61 or 'a'.

This function modifies the data and as I could understand that wasn't what the original question here was about.

toascii is available in C also - as are all functions in ctype.h (ctype.h is a C library include file).

If you just want to get the ascii value of a character, just use the char as an integer or cast it to int. All ascii characters should be in the range 0..127 so the 'toascii' function is useless to them, it clear the high bit but all ascii codes have that bit already cleared. What it does wrong is to clear the bit on codes that are outside the ascii range.

Some older systems used something referred to as a parity bit in which they transferred the ascii code over 8 bit and used that extra high bit as a parity bit. This was set or cleared so as to force the number of bits set to be odd or even.

'\0' is represented as 0x80 in odd and 0x00 in even parity.
'\1' is represented as 0x01 in odd and 0x81 in even parity.
'\2' is represented as 0x02 in odd and 0x82 in even parity.
'A' is represented as 0xc1 in odd and 0x41 in even parity.

This way when sending the character over a communication line the receiver could complain about parity error if the parity is not what you expect. This would catch many errors in communication if the line was unstable.

In modern times you seldom send one and one byte and have parity check on each. It isn't safe enough. Sending a block of bytes and have CRC-32 or CRC-16 check on them instead is both more compact and safer.

So parity checking - and therefore the need to clear the parity bit by using toascii() - is a thing of the past and isn't much used today.

To use toascii() to print the ascii code of a character is just doing a lot of work for no gain. Sure I can write a function like this, it will also compute the ascii character of the input, but who would use such a function?

int to_ascii(char c)
   // copy the character bit by bit to an int.
   int k = 0;

   for (int j = 0x80; j != 0; j >>= 1)
       k |= j & c;
   return k;

Yes, the function would return the ascii value but it would be a lot simpler to just say:

#define to_ascii(c) int(c)

to_ascii('A') is now the ascii value of c. It's even a macro so it won't be any function call.

All in all that function is just obfuscating the code so it is simpler and clearer to just write:

int x = c;


int x = to_ascii(c);

to make x get the ascii code of c.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.