I am trying to implemnt a simple XOR encoding on a string in C++11 (Visual Studio 2017).However, I am having some problems implementing it. The following code would be my standard (C) approach:
char* encode(char* input) {
for (unsigned int i = 0; i < strlen(input); i++) {
input[i] ^= 0xA;
}
return(input);
}
int main(void){
printf(encode(encode("Hello World!")));
}
But it is no valid code because the string ("Hello Wolrd!") is of type const char*.
I tried several things to fix the problem:
-
Cast the string to
(char*)
(i.e.,encode( (char*)"Hello World!")
) but it throws an exception when trying to XOR the first byte. -
Using a temp
char*
in my encoding function, which also resulted in stack coruption.char* encode(const char* input) { char temp[128]; strncpy(temp, input, 128); for (unsigned int i = 0; i < strlen(temp); i++) { temp[i] ^= 0xA; } return(temp); }
-
I tried using
std:string
but it also did not work out.
What am I missing?
Aucun commentaire:
Enregistrer un commentaire