简体   繁体   中英

how to convert ASCII code to char in c++?

In my class my teacher has gave me a homework about encrypt and decrypt password. In this homework, the algorithm is to find out how to decrypt to it's previous user has inputted.In fact, How to convert it from ASCII code back to char. Is there any way to do that? thanks.

This is the code:

#include<iostream>
using namespace std;

int main(){

    //reverse
    cout << "Please input password: ";
    cin >> pwd;
    int size = pwd.length();
    const int maxSize = 100;
    char pwdReversed[maxSize];
    int pwdEncrypted[maxSize];
    int j = 0;
    cout << "Reversed Password: ";
    for(int i = size - 1; i >= 0; i--){
        cout << pwd[i];

        pwdReversed[j] = pwd[i];
        pwdEncrypted[j] = (int)pwd[i];
        j++;


    }
    cout<< endl;
    //Output
    cout << "Encrypted pwd: ";
    for(int i = 0; i < j; i++){
        cout << pwdEncrypted[i];
    }
    cout << endl;

    return 0;
}

只需将其转换为char (在绝大多数架构上,使用的字符集与 ASCII 兼容)。

    cout << (char)pwdEncrypted[i];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM