Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将Ascii字符转换为整数_C++_Encryption_Ascii - Fatal编程技术网

C++ 将Ascii字符转换为整数

C++ 将Ascii字符转换为整数,c++,encryption,ascii,C++,Encryption,Ascii,嘿,伙计们,我这里有一个程序,基本上需要一个密钥来打开加密数据,但由于某种原因,每次我运行程序时,当转换Ascii数字为字符时,我得到的字符的Ascii值是错误的。例如,在下面的代码中,如果它尝试将Ascii值“50”转换为字符,我将得到“e”而不是“2”。任何帮助都将不胜感激 #include "stdafx.h" #include "iostream" #include "string" #include <cstdlib> #includ

嘿,伙计们,我这里有一个程序,基本上需要一个密钥来打开加密数据,但由于某种原因,每次我运行程序时,当转换Ascii数字为字符时,我得到的字符的Ascii值是错误的。例如,在下面的代码中,如果它尝试将Ascii值“50”转换为字符,我将得到“e”而不是“2”。任何帮助都将不胜感激

    #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]);
    }
#包括“stdafx.h”
#包括“iostream”
#包括“字符串”
#包括
#包括
#包括
使用名称空间std;
字符串键=“505352555”;
内华达州;
炭疽;
对于(int i=0;i
这是为感兴趣的人准备的完整代码

    #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;

}
#包括“stdafx.h”
#包括“iostream”
#包括“字符串”
#包括
#包括
#包括
使用名称空间std;
无效加密(字符串文本)
{
炭纤维;
int-ascivalue=0;
字符串键;
字符串编码;
srand((未签名)时间(0));
int random_integer=rand();

coutASCII数字字符以30hex(或48dec)开始。因此“0”=30hex。因此,要获得ASCII字符,必须添加“0”(30h)


'cout
number=ASCIIchar-'0';
ASCIIchar=number+'0';
请编辑您的问题以包含一个。请详细说明,当我尝试时,这是不起作用的。我想您最好详细说明当您尝试@DavidC.Rankin的建议hatbrakingzoot时发生了什么。关于
的内容已经不多了>编号=ASCISICAR’- 0’;< /C>。在我输入代码后,for循环结束时,它返回的几乎相同的东西,给或取零。虽然我对C++是比较新的,但是它很可能是我的一个错误。谢谢BCR解释,对我来说,它现在变得更有意义,尤其是因为我是C++新手。