468,765 Members | 1,117 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,765 developers. It's quick & easy.

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

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
6 8451
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
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
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
char c= 'a';

int i= c;
--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #5
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
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.

Similar topics

19 posts views Thread by Dan | last post: by
1 post views Thread by James Dean | last post: by
3 posts views Thread by Director - Minvent | last post: by
4 posts views Thread by meendar | last post: by
9 posts views Thread by =?Utf-8?B?RGFu?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.