September 1st, 2003, 12:06 AM
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.
September 1st, 2003, 01:13 AM
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++:
for (int i=0; i<127; i++)
ascii[i] = (char)(i + 1); // type-cast just for general purposes
September 1st, 2003, 12:31 PM
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.