Well, i've been trying to right a little program, that will go through all of the characters in a string (or in this case a char array) and advance their ASCII character code by so much, essentially a cipher.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
int main()
{
char charstring[256];
cout<<"Please input a 10 character long string...\n";
cin>>charstring;
for (int i = 0; i <= 256; i++) {
}
system("PAUSE");
return 0;
}
what can i do so that during the for loop, i can select the individual character (charstring[i]) and add or subtract from its ASCII number?
Are you looking to return the ASCII character from the ASCII character code, something like this?
cout << static_cast<char>(asciiNumber);
and is there a way to turn a character into a ASCII value?
I'm not quite sure I understand this statement. If you declare say: char characterOne; retrieving the ASCII Character Code (number) or the Character symbol is dependent on how you use the variable.
A char is not really a character. It's just an integer that happens to be interpreted as a character. For example, I can do this: char a=('A'+'6')/3; That's perfectly legal code.
The only moment when it matters whether something is a char or not is when printing with streams:
1 2
char a='A';
std::cout <<a;
That prints 'A' because the compiler automatically selects the overload of operator<< that better fits the situation. operator<<(char) happens to print character, not numbers.
If you want to print the actual value, you'd do this:
1 2
char a='A';
std::cout <<(int)a;
Note that that only applies to streams. If you do this:
1 2
char a='A';
printf("%d",a);
That will really print the value 65 (or whatever the encoding says). Even if a was declared as an int the result would be the same.