简体   繁体   English

如何在 C++ 中将 ASCII 代码转换为字符?

[英]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.在这个作业中,算法是找出如何解密为以前用户输入的。实际上,如何将其从ASCII码转换回字符。 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];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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