ASCII value of a character

Posted on 2003-03-07
Medium Priority
Last Modified: 2007-12-19
# 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++?????
Question by:kiranchandbv
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
  • Learn & ask questions
  • 2
LVL 10

Accepted Solution

makerp earned 300 total points
ID: 8087236
#include <iostream>
using namespace std;

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

Expert Comment

ID: 8087255
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 www.unicode.org 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.


Expert Comment

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

LVL 12

Expert Comment

ID: 8087511
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.


Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question