HatBreakingDaddy
HatBreakingDaddy

Reputation: 35

Converting an Ascii Character to an Integer

Hey guys I have a program here that basically needs a key to open encrypted data but for some reason every time I run the program when converting the Ascii numbers to characters I get characters with the wrong Ascii value. For example in the code below if it tried converting the Ascii Value '50' to a char I would get 'e' instead of '2'. Any help would be much appreciated.

    #include "stdafx.h"
    #include "iostream"
    #include "string"
    #include <cstdlib>
    #include <ctime>
    #include <algorithm>

    using namespace std;

    string key = "5053525055";
    int asciiValues;
    char asciiChars;
    for (int i = 0; i < key.length(); i += 2)
    {
        asciiValues = (int)(key[i] + key[i + 1]);
        asciiChars = (int)(key[i] + key[i + 1]);
    }

Here's the complete code for those interested.

    #include "stdafx.h"
    #include "iostream"
    #include "string"
    #include <cstdlib>
    #include <ctime>
    #include <algorithm>

    using namespace std;

    void encryption(string text)
    {
        char asciiChar;
        int asciiValue = 0;
        string key;
        string encoded;
        srand((unsigned)time(0));
        int random_integer = rand();
        cout << random_integer << endl;
        //Creates the key for the string.
        for (int i = 0; i < to_string(random_integer).length(); i++)
        {
           int asciiValue = (char)(to_string(random_integer)[i]);
           key = key + to_string(asciiValue);
           cout << asciiValue << endl;
        }
        int help = to_string(asciiValue).length();
        /*Function that converts the individual characters in the input
        string to AsciiValues and then puts them into the encoded data.*/
        for (int i = 0; i < text.length(); i++)
        {
            int asciiValue = char(text[i]) + random_integer;
            encoded = encoded + to_string(asciiValue) + ".";
        }

     cout << "Encrypted data: " << encoded << endl;
     cout << "Your key for this encoded data is " << key << endl;
    }

    void decryption(string text, string key)
    {
    char asciiChars;
    int asciiValues;
    int number;
    string qkey;
    string decoded;
    /*for (int i = 0; i < to_string(random_integer).length(); i++)
    {
        int asciiValue = (char)(to_string(random_integer)[i]);
        key = key + to_string(asciiValue);
        cout << asciiValue << endl;
    }*/
    for (int i = 0; i < key.length(); i += 2)
    {
        asciiValues = (int)(key[i] + key[i + 1]);
        asciiChars = (int)(key[i] + key[i + 1]);
        number = asciiChars - '0';
        cout << number << endl;
    }
    cin >> qkey;
    }


    int main()
    {
    string answer;
    int question = 0;
    string vkey;
    ask:
    cout << "Would you like to:\nEncrypt Data[1]\nDecrypt Data[2]\nExit[3]" << 
    endl;

    cin >> question;
    if (to_string(question) != "1"&&to_string(question) != 
    "2"&&to_string(question) != "3")
    {
        goto ask;
    }
    else if (to_string(question) == "1")
    {
        while (answer.length() > 1000 || answer.length() < 1)
    {
        cout << "Please enter a string that has a length of 1 to 1000 
        characters. ";
        cin >> answer;
        cout << endl;
    }
    encryption(answer);
    cin >> answer;
    goto ask;
    }
    else if (to_string(question) == "2")
    {
        cout << "Please enter the string you would like decrypted. ";
        cin >> answer;
        cout << endl;
        cout << "Now please enter the key for the string. ";
        cin >> vkey;
        cout << endl;
        decryption(answer, vkey);
     }

return 0;

}

Upvotes: 1

Views: 813

Answers (1)

bcr
bcr

Reputation: 950

ASCII number characters begin at 30hex (or 48dec). Therefore '0' = 30hex. So, to get an ASCII character, you must add '0' (30h).

'cout << 5 + '0'; // 53dec (35hex)

ASCII Chart

Upvotes: 3

Related Questions