【发布时间】:2021-08-11 00:20:39
【问题描述】:
我在处理字符串数组时遇到了“范围检查错误”。据我了解,这意味着我正在使用数组中不存在的索引。
我的头文件中的类和函数:
AnsiString toBin(int n) {
AnsiString zero = "0";
int len;
AnsiString result;
while (n != 0) {
result += (n % 2 == 0 ? "0" : "1");
n /= 2;
}
len = result.Length();
while (result.Length() < 8)
result = zero + result;
return result;
}
int toInt(AnsiString bin) {
int start = 1;
int result = 0;
for (int i = 0; i < 8; i++) {
result += (int)bin[i] * start;
start * 2;
}
return result;
}
class enc_dec {
private:
AnsiString stext;
AnsiString skey;
public:
enc_dec(AnsiString t, AnsiString k) {
stext = t;
skey = k;
}
AnsiString XOR() {
if (skey == "") {
Application->MessageBox(L"Error!", L"No Key", MB_OK);
return "";
}
AnsiString cry = "";
int key = skey[1] + 0;
AnsiString keyBin = toBin(key);
AnsiString temp = "";
// AnsiString output[stext.Length()];
std::vector<AnsiString>output;
for (int i = 1; i < stext.Length(); i++) {
temp = toBin(stext[i]);
for (int j = 1; j < 8; j++) {
if (temp[j] == keyBin[j])
temp[j] = '0';
else
temp[j] = '1';
}
output.push_back(temp[i]);
}
for (int i = 0; i < stext.Length(); i++)
cry += char(toInt(output[i]));
return cry;
}
};
我的.cpp 文件:
void __fastcall TForm1::Button1Click(TObject *Sender){
enc_dec temp(Edit1->Text,Edit2->Text);
temp.XOR();
}
设计:
我在更新前发现了一个错误,“范围错误”尚未解决。
【问题讨论】:
标签: c++ c c++builder rad-studio