将字符数组转换为字符串时出现奇怪的异常

时间:2014-07-08 05:23:32

标签: c++ arrays string char type-conversion

我正在尝试构建一个利用替换表和密钥生成密码/加密文本的程序。到目前为止,我已成功将加密文本存储在char数组中。如果我使用for循环遍历文本,它每次都能完美运行。但是,我需要将加密文本作为字符串返回。这是我使用的方法......

string ciphertext = cipherArray; //cipherArray is of type char

当我尝试cout密文时,它会有时正常输出,有时它会显示一些正确的字母,然后是奇怪的符号。当我使用for循环打印数组时,它工作正常,但将其转换为字符串给我带来了问题。它似乎是在存储从char转换为字符串时超出范围的项目。

这是完整的程序:(问题在加密方法定义下)

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

//=============================================================================
//                          -Cipher Class-
//
class Cipher {
    public:
        Cipher();
        Cipher(string key);
        Cipher(string key, string message);

        void newKey();
        string inputMessage();
        string encipher(string message);
        string decipher(string message);
        string getPlainText() const;
        string getCipherText() const;

    private:
        void initAlphabet();
        void initTable();

        char alphabet[26];
        char table[26][26];
        string key;
        string plaintext;
        string ciphertext;
};

//=============================================================================
//                          -Main-
//
int main()  {
    Cipher myCipher;

    cout << "Key: ";
    myCipher.newKey();
    cout << endl;

    cout << "Message: ";
    myCipher.encipher(myCipher.inputMessage());


    return 0;
}

//=============================================================================
//                          -Constructor-
//
Cipher::Cipher() {
    initAlphabet();
    initTable();
}

//=============================================================================
//                          -Initialize Alphabet-
//
void Cipher::initAlphabet() {

    char alphaStore[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m',
                         'n','o', 'p','q','r','s','t','u','v','w','x','y','z'};

    for (int i = 0; i < 26; i++)
        alphabet[i] = alphaStore[i];
}

//=============================================================================
//                          -Initialize Table-
//
void Cipher::initTable() {

    int alphaIndex = 0;

    for (int index1 = 0; index1 < 26; index1++) {
        for (int index2 = 0; index2 < 26; index2++) {   

            if ((index1 + index2) < 26) {
                alphaIndex = index1 + index2;
                table[index1][index2] = alphabet[alphaIndex];
            }
            else
                alphaIndex = 0;

            while (((index1 + index2) > 25) && index2 < 26) {
                table[index1][index2] = alphabet[alphaIndex];
                index2++;
                alphaIndex++;
            }           
        }               
    }
}


//=============================================================================
//                          -Input Message-
//
string Cipher::inputMessage() {
    cin >> plaintext;
    return plaintext;
}

//=============================================================================
//                          -New Key-
//
void Cipher::newKey() {
    cin >> key;
}

//=============================================================================
//                          -Encipher-
//
string Cipher::encipher(string message) {

    int iTableRow, iTableCol, i;
    unsigned int iKey, iMsg;
    int keyValStorage[key.length()];
    int msgValStorage[message.length()];
    char cipherArray[message.length()];    

    for (iKey = 0; iKey < key.length(); iKey++)
        for (iTableCol = 0; iTableCol < 26; iTableCol++)
            if (key[iKey] == table[0][iTableCol]) {
                keyValStorage[iKey] = iTableCol;
                iTableCol = 26;
            }

    for (iMsg = 0; iMsg < message.length(); iMsg++)
        for (iTableRow = 0; iTableRow < 26; iTableRow++)
            if (message[iMsg] == table [0][iTableRow]){
                msgValStorage[iMsg] = iTableRow;
                iTableRow = 26;
            }

    for (iKey = 0, iMsg = 0; iMsg < message.length(); iKey++, iMsg++) {
        if (iKey > (key.length() - 1))
            iKey = 0;
        cipherArray[iMsg] = table[keyValStorage[iKey]][msgValStorage[iMsg]];
    }         

    for (iKey = 0; iKey < message.length(); iKey++) 
        cout << cipherArray[iKey] << " ";   //This prints the encrypted text fine

    ciphertext = cipherArray; //This produces anomalies in the output.
    cout << ciphertext;

    return "placeholder";             

}

1 个答案:

答案 0 :(得分:0)

std:string有一个constructor,它带有char缓冲区和字符串长度。只需使用:

string ciphertext = string(cipherArray, /*length_of_cipherArray_here*/);

甚至更短:

string ciphertext(cipherArray, /*length_of_cipherArray_here*/);

当然,这假设cipherArray是char[]char*