I've been getting "Range check error"s while working with string arrays. To my understanding, this means I am using indexes that don't exist in my array.
My class and functions in my header file:
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;
}
};
My .cpp
file:
void __fastcall TForm1::Button1Click(TObject *Sender){
enc_dec temp(Edit1->Text,Edit2->Text);
temp.XOR();
}
The design:
I found a mistake from before updating, The "Range Error" has not been resolved.