Ascii character conversion

Started by
13 comments, last by Qw3r7yU10p! 19 years, 5 months ago
char c=97;

Have you tried that? It will work.
97 and 'a' are the same thing... It's just how you look at it.
Advertisement
...

* head explodes *

A char is just a number. It holds the ascii value for whatever character you have. The compiler doesn't care if you use 'a' or 97. They're exactly the same thing. You can use either one. It's really just a convenient shortcut so that you don't need to remember the codes for all the characters, just like you can write "Hi!" instead of {72, 105, 33, 0}. It's makes no difference to the compiler, they're exactly the same.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Just wanted to mention that 'a' isn't necessarily equal to 97.
A friend of mine has to deal with IBM mainframes on a regular basis..

I'd better add that characters constants are always integers, wouldn't want to confuse things further ;)
Hmm, now I'm confused. I have this code:

#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <time.h>
int main()
{
srand(time(NULL));
char test[100];
char buff;
int x = 0;
cout << "Enter text to encrypt: ";
cin >> test;
ofstream text ("text.txt");
while (x < 100)
{
text << test[x];
buff = (97 + rand(26));
text << buff;
buff = (97 + rand(26));
text << buff;
x++;
}

system("PAUSE");
text.close();
return 0;
}

The first time I run it, it works fine. However, if I run it again without deleting "text.txt" first, "text.txt" becomes filled with asian symbols.
char test[100];

This defines an array of chars. It doesn't set them to anything. It will be whatever values are in the computers memory at that point. It could be garbage it could be zeros. Whatever.


cin >> test;

This will copy a character string into test. However long the string is, if it less than 99 characters then the garbage values mentioned above will still be garbage.

You could initialise char test[100] by doing the following:

char test[100] = {'J'}

This will fill all the characters in the array with J (for Jared), so you can see it's uninitiated.

This topic is closed to new replies.

Advertisement