Turning int into an ASCII character

SOLVED


Hello,

In this program, I open a text file with a username and password on two lines which are in ASCII value and turn them back into characters so they become words again. In my case, my numbers are (98 108 117 101 122 111 114) and I want to turn it back into the word (bluezor) (and leave out the white-spaces along the way?). I'm having trouble changing it back to letters.

logOnCharacter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
void logOnCharacter()
{
    std::string username = "";
    std::string password = "";

    //initialises the arrays to store username and password and sets every slot NULL
    char usernameArray_char[100];
    char passwordArray_char[100];
    int usernameArray_int[100];
    int passwordArray_int[100];
    for (int i = 0; i < 100; i++)
    {
        usernameArray_char[i] = ' ';
        passwordArray_char[i] = ' ';
        usernameArray_int[i] = 0;
        passwordArray_int[i] = 0;
    }

    ClearScreen();

    std::ifstream charFile ("gameData.txt");
    if (charFile.is_open())
    {
        getline(charFile, username); //gets first line and enters it as username
        getline(charFile, password); //gets second line and enters it as password

        for (int i = 0; i < username.length(); i++)   //** I think I need help here **
        {
            usernameArray_char[i] = username[i];
            std::cout << usernameArray_char[i];
        }

        charFile.close();
    }
    else
    {
        std::cout << "Unable to locate or open gameData.txt." << std::endl;
        std::cout << "Please create a new game file by running the program again" << std::endl;
    }
}


This outputs the ASCII values (98 108 117 101 122 111 114) correctly, so this is where it starts to get wrong I believe?

1
2
3
4
5
        for (int i = 0; i < username.length(); i++)   //** I think I need help here **
        {
            usernameArray_char[i] = username[i];
            std::cout << char(usernameArray_char[i]);
        }

When I try to do this to change the value into a character it still stays as an ASCII value so I don't know why.
Last edited on
Your code works for me.


What kind of compiler do you use?

Borland c? Because the 'ClearScreen();' is built in the borland c.

You should use more modern compiler. For example:

-Visual studio express (+compiler)
- QTCreator with mingw (4.4)
-bcc 5.5 compiler (also free)
Last edited on
I use Code::Blocks and ClearScreen() is in a header file made by Duoas (see: Articles - How to Clear Screen). For me when I do the second code all I get is a bunch of random numbers together as opposed to the letters I am expecting.
This code woks for me correctly:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <fstream>
using namespace std;



int main()
{
	string text = "hello";
	char array[5];
	
	for ( int i = 0; i < 5; i++ )array[i] = text[i];
	
	for ( int i = 0; i < 5; i++ )cout << array[i];
	return 0;
}



Please try it.

If this code doesn't work then it is possible you should use change the compiler to mingw 4.4!

Look the 'tdm mingw' on google
Last edited on
The problem is, i'm not trying to change from letters to ASCII value or letters to a character array. I'm changing each individual ASCII value on a line (each value / character is separated by one white-space) to its letter. As said, i'm trying to change (98 108 117 101 122 111 114) back to (bluezor) but (?)it's not working like I would think it would work.

Thanks for trying to help though.
Last edited on
It isn't that difficult:
1
2
3
4
int array[7] = { 98, 108, 117, 101, 122, 111, 114 };
string str;
for ( int i = 0; i < 7; i++ )
        str += array[i];
That doesn't work, all that does is put the integers into the string which I already have. Instead of the integers, I'm trying to get the ASCII value for the integers.
Sorry if i'm not clear :x I suck at explaining stuff or asking questions
An ASCII value is an integer...
>.< Well umm i'm trying to change that ASCII value to a letter I guess. If I do something like -
1
2
3
4
5
6
7
        for (int i = 0; i < usernameLine.length(); i++)
        {
            usernameArray_char[i] = usernameLine[i];
            usernameArray_int[i] = usernameArray_char[i];
            username += char(usernameArray_int[i]);
        }
        std::cout << username;


Username still gets the initial values of (98 108 117 101 122 111 114) instead of the char value.
Is usernameLine a string formatted like this: "98 108 117 101 122 111 114" ?
If so, you need to convert it to a sequence of numbers ( "98" is a sequence of the characters '9' and '8' )
eg:
1
2
3
4
5
6
string line = "98 108 117 101 122 111 114";
string result;
stringstream ss ( line );
int temp;
while ( ss >> temp ) // read next integer
    result += temp; // add it as char in the string 
1
2
3
4
5
6
        while (usernameLine_ss >> username_int)
        {
            username += username_int;
        }
        
        std::cout << username;

Worked. Bazzy you're a legend xD

Thanks
Topic archived. No new replies allowed.