将数值映射到字符

时间:2012-06-17 20:24:17

标签: visual-c++

使用C ++,我试图编写一个加密字符串的程序。我必须将字母表中的每个字母映射到一个数值:例如,a = 0,b = 1,c = 2,依此类推。到目前为止,我已经创建了一个void函数,它将一个字符串作为参数,并使用switch语句输出值。问题是,值是字符,而不是整数,我不能使用数学运算符来改变它们。 我的源代码如下:

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <string>
#include <cctype>

using namespace std;

void mapped_string(string in)
{   string mapped_string;
    int length = in.length();

    for (int i = 0; i < length; i++)
    {   
        switch (in[i])
        {
        case 'a':
            cout<<"0";
            break;
        case 'b':
            cout<<"1";
            break;
        case 'c':
            cout<<"2";
            break;
        case 'd':
            cout<<"3";
            break;
        case 'e':
            cout<<"4";
            break;
        case 'f':
            cout<<"5";
            break;
        case 'g':
            cout<<"6";
            break;
        case 'h':
            cout<<"7";
            break;
        case 'i':
            cout<<"8";
            break;
        case 'j':
            cout<<"9";
            break;
        case 'k':
            cout<<"10";
            break;
        case 'l':
            cout<<"11";
            break;
        case 'm':
            cout<<"12";
            break;
        case 'n':
            cout<<"13";
            break;
        case 'o':
            cout<<"14";
            break;
        case 'p':
            cout<<"15";
            break;
        case 'q':
            cout<<"16";
            break;
        case 'r':
            cout<<"17";
            break;
        case 's':
            cout<<"18";
            break;
        case 't':
            cout<<"19";
            break;

        default:
            cout<< in[i];
        }
    }
}

int main()
{   string str1 = "Hello";

    mapped_string(str1);



        cout    << "Press any key to exit." << endl;
        cin.ignore(2);
        return 0;
}

我需要这个函数将字符串中的每个char映射到一个int值,并将int值存储在一个名为mapped_string的新字符串中。

3 个答案:

答案 0 :(得分:0)

//stringstream is used as string 'builder'.
std::stringstream storedstr;

for (int i = 0; i < length; i++)
{   
    //As op's code but without the lengthy switch statement. Just cast to get an int.
    int intEquiv = (int)(in[i]);
    if (in[i] > 'z' || in[i] < 'a') intEquiv = (int)(in[i]) - 'a'
    storedstr << intEquiv;

    //Show what we are adding, like in original loop;
    std::stringstream ss;
    ss << intEquiv;
    std::cout << ss.str();
}
mapped_string = storedstr.str();

答案 1 :(得分:0)

C / C ++中的字符具有数值,ASCII格式,'A'为65,'B'为66 ...'a'为97,'b'为98等等,因此您可以应用数学操作员,可能更容易简单地坚持使用众所周知的ASCII格式,而不是创建自己的,http://en.wikipedia.org/wiki/ASCII

string str1 = "Hello";
for (int index = 0; index < str1.length(); index++)
    cout << "char: " << str1[index] << " ascii numeric values: " << (int)(str1[index]) << endl;

打印:

char: H ascii numeric values: 72
char: e ascii numeric values: 101
char: l ascii numeric values: 108
char: l ascii numeric values: 108
char: o ascii numeric values: 111

一个简单/天真的加密方案就是简单地将字符移动一定量(移位密码),但是在使用加密方案时,您应该始终注意溢出,具体取决于您正在实施的方案。

  string str1 = "Hello", cipher = "";    
  for (int index = 0; index < str1.length(); index++)
      cipher += (char)(str1[index] + 10);
  cout << "original: " << str1 << " cipher " << cipher << endl;

打印:

 original: Hello cipher Rovvy 

取回原件只需减去偏移量。

答案 2 :(得分:0)

使用C ++ 11:

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

string mapped_string(string in, char password)
{
    transform(in.begin(), in.end(), in.begin(), [password](char& x){ return x ^ password; });
    return in;
}

int main(int argc, char* argv[])
{
    char password = '1';
    string str1 = "Hello";
    string str2 = mapped_string(str1, password); // encrypt str1
    string str3 = mapped_string(str2, password); // decrypt str2
    cout << "Original: " << str1 << endl;
    cout << "Encrypt:  " << str2 << endl;
    cout << "Decrypt:  " << str3 << endl;
    cin.ignore();
    return 0;
}

输出:
原文:你好 加密:yT]]
解密:你好