#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    39
    Rep Power
    11

    Question about using ASCII characters in C++


    Hello, I would like to create an array of ascii characters 1 - 127. Are there any built in functions in C++ to automatically retrieve ascii values? I ask because I dont know how to type many of these characters. Also, it would be much easier than having to manually add each one. Any information would be appreciated.

    thanks!

    --Musa
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,156
    Rep Power
    2222
    This is one of those questions where we're not quite sure what's being asked. If you are very new to C/C++, then you may not be aware that C/C++ don't really differentiate between character data and their ASCII values like Pascal and BASIC do. You can treat a character as a character or as a number (its ASCII value) and you can treat a number as a number or as a character. In fact, many of the character-handling functions in the C standard library take ints instead of chars.

    Therefore, to load the ASCII values 1-127 into an array indexed from 0 to 126 (sorry, that's how arrays come in C and you did not specify starting with any different index), using C++:
    Code:
    char ascii[127];
    
    for (int i=0; i<127; i++)
        ascii[i] = (char)(i + 1);  // type-cast just for general purposes
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    39
    Rep Power
    11
    thanks for the reply, but I found the answer. I am not really new to C++, I just forgot how chars were interpreted. thanks for the help anyways.

    --Musa

IMN logo majestic logo threadwatch logo seochat tools logo