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

Discussion in 'C++' started by Haas, Apr 9, 2005.

  1. Haas

    Haas Guest

    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
     
    Haas, Apr 9, 2005
    #1
    1. Advertising

  2. Haas

    Guest

    Re: 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;
    -}
     
    , Apr 9, 2005
    #2
    1. Advertising

  3. Haas

    Rolf Magnus Guest

    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.
     
    Rolf Magnus, Apr 9, 2005
    #3
  4. 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
     
    Ioannis Vranos, Apr 9, 2005
    #4
  5. Ioannis Vranos, Apr 9, 2005
    #5
  6. 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:)
    The Incredible Brightness of Seeing, a Home Theater weblog
    http://www.anaze.us/HomeTheater
     
    Jonathan Arnold, Apr 11, 2005
    #6
  7. 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
     
    Ioannis Vranos, Apr 11, 2005
    #7
  8. Haas

    Sambit

    Joined:
    Oct 26, 2011
    Messages:
    2
    Location:
    Orissa
    #include<iostream.h>
    main()
    {
    int a='b' ; // U may write any character in place of 'b', i.e.'C','H','s',etc.
    cout<<a;
    }
    ??try this....
     
    Sambit, Oct 26, 2011
    #8
  9. Haas

    Sambit

    Joined:
    Oct 26, 2011
    Messages:
    2
    Location:
    Orissa
    #include<iostream.h>
    main()
    {
    int a='b' ; // U may write any character in place of 'b', i.e.'C','H','s',etc.
    cout<<a;
    }
    //??try this....
     
    Sambit, Oct 26, 2011
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lovecreatesbeauty
    Replies:
    1
    Views:
    1,099
    Ian Collins
    May 9, 2006
  2. Jim Langston
    Replies:
    5
    Views:
    425
    Jonathan Mcdougall
    May 6, 2006
  3. Aneesh E Warrier
    Replies:
    6
    Views:
    1,535
  4. sajit

    Ascii value of char. in C#

    sajit, Apr 7, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    2,072
    Ben Rush
    Apr 7, 2007
  5. Replies:
    1
    Views:
    543
    7stud
    Apr 10, 2007
Loading...

Share This Page