Go Down

Topic: error: invalid types 'char[int]' for array subscript (Read 432 times) previous topic - next topic

Munkeeworks

I found this a while back not sure if it was originally written in c or c++... been trying to convert it to work with Arduino. I am just trying to make this bit work and will add some more in the future.

I keep getting the error: invalid types 'char[int]' for array subscript  on this line " encrypted += original[temp] ^ (int(key) + temp) % 255;"


Code: [Select]

char original = 'put me in a box, a box of worms';
char key = 'z';
char end;
char getKey;
int temp = 0;
String encrypted = "";
String unencrypt = "";

void setup(){
  Serial.begin(9600);
  end = original ^ key;
  getKey = original ^ end;
  Serial.println("Ready");
  delay(1000);

}
void loop(){
  Serial.print(original);
  c();
  delay(1000);
  d();
  delay(5000);
}
void c(){// add variable in future
  temp = 0;
  while(temp < sizeof(original)){
    encrypted += original[temp] ^ (int(key) + temp) % 255;
    temp++;
  }
  Serial.print("Encrypt: ");
  Serial.println(encrypted);

}
void d(){
  temp = 0;
  while(temp < sizeof(original)){
    unencrypt += encrypted[temp] ^ (int(key) + temp) % 255;
    temp++;
  }
  Serial.print("Decrypt: ");
  Serial.println(unencrypt);
}

robtillaart

The first bugs are is in the very first line of code, that should read
Code: [Select]
char original[] = "put me in a box, a box of worms";

read the pages about array's in C / C++ in the tutorial section, that should help you to fix the code



Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Munkeeworks

Thanks,
I actually found a better library that serves my purpose

michinyon

It's probably a good idea to avoid using Strings.  Appending those characters to a String will use a lot of memory.

Go Up