1

I am new to programming and I have to write the code which will print the ASCII value of an input character. Is there any other way to reduce the 52 (26 for lowercase and 26 for upper) if-else statements or 52 switch-cases ?

Raj Kumar
  • 67
  • 9

3 Answers3

2

Because it's not clear what you are trying to do, your question is not very well written. However, I think you need to take a look at typecasting.

Try this statement to convert the character to the corresponding ASCII value:

int output = (int)inputCharacter;
J. Allan
  • 1,418
  • 1
  • 12
  • 23
1

Yes, just write the value of that character.

(int)ch;

Note that you may need to write appropriate code depending on whether or not you're using Unicode, and also depending on what method you are printing (usually best to include a little code).

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
0

You can cast the char as an int, or any other numeric type. For example,
(int) 'A'

will evaluate to 65.

  • `65` only with ascii, there exists other character encoding as [EBCDIC](https://en.wikipedia.org/wiki/EBCDIC) with different value. – Jarod42 Aug 02 '16 at 20:13