Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Haas
Guest
 
Posts: n/a
#1: Jul 23 '05
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

wittempj@hotmail.com
Guest
 
Posts: n/a
#2: Jul 23 '05

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


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

- return 0;
-}

Rolf Magnus
Guest
 
Posts: n/a
#3: Jul 23 '05

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


Haas wrote:
[color=blue]
> Hello folks,
>
> I just started a C++ course. An exercise is to give the ASCII value of
> a character read from keyboard.[/color]

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.
[color=blue]
> I place the input from the keyboard in a char-variable. How can i get the
> ASCII value of this char?[/color]

It already has that value. char is nothing else than a small integer.
[color=blue]
> Is there a built-in function?[/color]

No such function needed.
[color=blue]
> Or should i check the full table?[/color]

Not needed if your system uses an ASCII character set.

Ioannis Vranos
Guest
 
Posts: n/a
#4: Jul 23 '05

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


Haas wrote:
[color=blue]
> 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?[/color]


char c;

int i= c;



--
Ioannis Vranos

http://www23.brinkster.com/noicys
Ioannis Vranos
Guest
 
Posts: n/a
#5: Jul 23 '05

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


char c= 'a';

int i= c;


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jonathan Arnold
Guest
 
Posts: n/a
#6: Jul 23 '05

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


Ioannis Vranos wrote:[color=blue]
> char c= 'a';
>
> int i= c;[/color]

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:jdarnold@buddydog.org)
The Incredible Brightness of Seeing, a Home Theater weblog
http://www.anaze.us/HomeTheater
Ioannis Vranos
Guest
 
Posts: n/a
#7: Jul 23 '05

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


Jonathan Arnold wrote:
[color=blue][color=green]
>> char c= 'a';
>>
>> int i= c;[/color]
>
>
> 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.[/color]


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
Closed Thread