-
Xor char*
I want to xor string with prepared key before :
Code:
unsigned char key[8];
char *text;
text = "testtesttesttest";
srand ( time(NULL) );
int lowest=0, highest=255;
int range=(highest-lowest)+1;
for(int index=0; index<8; index++){
key[index] = lowest+int(range*rand()/(RAND_MAX + 1.0));
}
for(int a=0; text[a] != NULL; a++)
{
text[a] ^= key[a%8];
}
I was programming in delphi before and i had byte type which is unsigned char here as i read. Conversion was pretty easy but don't know how to do that in C++.
-
What's the problem? The code seems to be correct, despite of the fact that:
Code:
char *text;
text = "testtesttesttest";
Should be:
Code:
char text[] = "testtesttesttest";
-
Better yet,
Code:
char *text;
text = "testtesttesttest";
...can be changed to...
Code:
std::string text = "testtesttesttest";
-
"Incorrect" is not the word for it as it is not incorrect to use a null-terminated character array, it's just possibly more error prone.
I've been seeing a loss of distinction between fact and opinion lately from Elysia, which disappoints me a little.
FYI, it's like the distinction between array [0..X] of char and string in Delphi, though in C++ you don't need to use StrPas to convert from array of char to string.