c++ - 将 Ascii 字符转换为整数

标签 c++ encryption ascii

大家好,我这里有一个程序,基本上需要一个 key 才能打开加密数据,但出于某种原因,每次我在将 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]);
    }

这里是为那些感兴趣的人提供的完整代码。

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

}

最佳答案

ASCII 数字字符从 30 十六进制(或 48 十进制)开始。因此,'0' = 30 十六进制。做,要获得 ASCII 字符,您必须添加 '0' (30h)。

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

ASCII Chart

关于c++ - 将 Ascii 字符转换为整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49765683/

相关文章:

bash - 删除 Bash 中无效的非 ASCII 字符

string - 在 MATLAB 中将 ASCII 码转换为字符串

node.js - NodeJS 服务器文本哈希的安全性

android - 在 Android 上保持 TensorFlow 模型加密

c++ - 如何将整数转换为其等效的 ascii

c++ - C++ 什么时候调用作为类成员的对象的构造函数?

python - 无法找到使用 jwcrypto 在 python 中解密 JWE token (但在 ASP.Net 中创建)的方法

python - Pybind11 和 std::vector——如何使用胶囊释放数据?

c++ - MFC 编辑框 - 每次击键多个字符?

c++ - 重载全局 operator new(非 POD)