Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 876
  • Last Modified:

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++?????
0
kiranchandbv
Asked:
kiranchandbv
  • 2
1 Solution
 
makerpCommented:
#include <iostream>
using namespace std;

int main()
{
  char ch = 'A';
  cout<<"ASCII value of "<<ch<<" is "<<(int)ch<<endl;
  return 1;
}
0
 
SalteCommented:
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.

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

regards
ramesh
0
 
SalteCommented:
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.

etc...
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;

than

int x = to_ascii(c);

to make x get the ascii code of c.

Alf
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now