By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,407 Members | 920 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,407 IT Pros & Developers. It's quick & easy.

[c++] getting the ASCII value of a char

P: n/a
Hello folks,

I just started a C++ course. An exercise is to give the ASCII value of
a character read from keyboard. I place the input from the keyboard in
a char-variable. How can i get the ASCII value of this char? Is there
a built-in function? Or should i check the full table?

Thanks.
Jasper
Jul 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
This is it...
-#include <iostream>
-
-int main()
-{
- char a;
- std::cin >> a;
- std::cout << a << " " << int(a) << std::endl;-

- return 0;
-}

Jul 23 '05 #2

P: n/a
Haas wrote:
Hello folks,

I just started a C++ course. An exercise is to give the ASCII value of
a character read from keyboard.
I assume your implementation uses ASCII or something based on that as a
character set? C++ doesn't define the character set to be used.
I place the input from the keyboard in a char-variable. How can i get the
ASCII value of this char?
It already has that value. char is nothing else than a small integer.
Is there a built-in function?
No such function needed.
Or should i check the full table?


Not needed if your system uses an ASCII character set.

Jul 23 '05 #3

P: n/a
Haas wrote:
Hello folks,

I just started a C++ course. An exercise is to give the ASCII value of
a character read from keyboard. I place the input from the keyboard in
a char-variable. How can i get the ASCII value of this char? Is there
a built-in function? Or should i check the full table?

char c;

int i= c;

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #4

P: n/a
char c= 'a';

int i= c;
--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #5

P: n/a
Ioannis Vranos wrote:
char c= 'a';

int i= c;


You don't need to do this, unless there is an overwhelming
desire to have the value as a signed int. char is merely
a (usually) smaller int.

--
Jonathan Arnold (mailto:jd******@buddydog.org)
The Incredible Brightness of Seeing, a Home Theater weblog
http://www.anaze.us/HomeTheater
Jul 23 '05 #6

P: n/a
Jonathan Arnold wrote:
char c= 'a';

int i= c;

You don't need to do this, unless there is an overwhelming
desire to have the value as a signed int. char is merely
a (usually) smaller int.

This is a nice way with which a newcomer in C++ can "get the ASCII value of a character".
I think it is better than be told to cast the char to int when passed to cout.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.