在将字符数组转换为字符串时出现奇怪的异常
Strange anomalies when converting an array of chars to a string
我正在尝试构建一个程序,该程序利用替换表和密钥来生成密码/加密文本。到目前为止,我已经成功地将加密文本存储在一个char数组中。如果我使用for循环遍历文本,它每次都能完美地工作。但是,我需要将加密文本作为字符串返回。这是我使用的方法…
string ciphertext = cipherArray; //cipherArray is of type char
当我尝试计算密文时,它有时会正常输出,有时会显示一些正确的字母,然后是奇怪的符号。当我使用for循环来打印数组时,它工作得很好,但是将它转换为字符串会给我带来问题。当它从字符转换为字符串时,似乎它正在存储超出范围的项。
下面是完整的程序:(问题在加密方法定义下)
#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";
}
std:string
有一个构造函数,它接受char
缓冲区和字符串长度。只使用:
string ciphertext = string(cipherArray, /*length_of_cipherArray_here*/);
或更短:
string ciphertext(cipherArray, /*length_of_cipherArray_here*/);
当然,这里假设cipherArray是char[]
或char*
。
相关文章:
- 在 c++ 中将集合转换为向量和向量转换为字符串时出现运行时异常
- std::out_of_range字符串比较中的异常
- 字符串的异常行为会追加属性 C++
- 将异常字符串解析为向量上的浮点运算?
- C++ 异常从字符串转换为c_str会创建垃圾字符
- 将 GetLastError() 转换为带有错误字符串的异常
- 在字符串类上的成员函数和out_of_range异常处理
- 尝试编写我自己的字符串类在 gcc 中出现异常
- 如何避免字符串到整数转换情况下的无效参数异常
- 由于字符串到JSON转换而产生的异常如何解决
- 异常和 C 样式字符串
- 附加性std字符串时,bad_Alloc异常
- 引发未经处理的异常:读取访问冲突。这>字符串被0x1C6F112
- 多个字符串异常处理
- boost::p rogram_option::store 在选项字符串包含混合语言字符时引发异常
- 如何通过包含要打印的字符串的类函数打印异常
- 关于多线程应用程序中的 STL 字符串异常
- 字符串转换错误:异常还是错误代码
- std::out_of_range在字符串向量中搜索匹配项时发生异常
- 将值分配给字符串向量时的 Cygwin 异常