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
?
Asked
Active
Viewed 913 times
1

Raj Kumar
- 67
- 9
-
1For a solution to this concrete problem see [this answer](http://stackoverflow.com/a/5030086/1051764). – TerraPass Aug 02 '16 at 18:42
-
Thanks @TerraPass it works :) – Raj Kumar Aug 02 '16 at 18:48
-
1@FirstStep sorry for the confusion, I have updated my question – Raj Kumar Aug 02 '16 at 18:49
3 Answers
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

Fawad Akhtar
- 36
- 3
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.

Shan Tulshi
- 11
-
`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