Thread: Getting ASCII value

  1. #1
    rm3
    Guest

    Getting ASCII value

    What function and header file does one use to get the ascii value of a character in a string?
    For example
    char st1[16] = "Hello, world.";
    int t = 'ascii value of' st1[4];

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Well

    Include iostream.h. You could also use printf(); if you prefer that.

    To get the ASCII-value (in this case 111):

    int t=st1[4];
    cout << t;

    To get the character (in this case the 'o'):

    int t=st1[4];
    cout << (char)t;
    Last edited by Magos; 11-22-2001 at 12:17 PM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    rm3
    Guest

    Thanks

    That works great, thank you.

    Now I was wondering how to put the ASCII value back into a string as a character.

    Such as~

    int a = 65;
    char st[16];
    st[16] = 'character representation of' (a);

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Hmm, oh well

    Oh, well, why not?

    Code:
    
    char Str[8]; //Create a string (array)
    Str[0]='A'; //Set element 0 to A
    Str[1]=45; //Set element 1 to ASCII-value 45
    
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    8
    There is a simpler function that does this.
    Ex:
    #include <conio.h>
    ...............................
    int ascii;
    char ch;
    ascii=getch();
    This should do it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ASCII character with ASCII value 0 and 32
    By hitesh_best in forum C Programming
    Replies: 4
    Last Post: 07-24-2007, 09:45 AM
  2. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  3. Office access in C/C++ NOT VC++!! :)
    By skawky in forum C++ Programming
    Replies: 1
    Last Post: 05-26-2005, 01:43 PM
  4. ascii values for keys
    By acid45 in forum C Programming
    Replies: 2
    Last Post: 05-12-2003, 07:13 AM
  5. Checking ascii values of char input
    By yank in forum C Programming
    Replies: 2
    Last Post: 04-29-2003, 07:49 AM