I just got completely bamboozled. Ive been searching for hours why i cant convert a string to a PUCHAR (unsigned char*). Its weird, but for some reason the windows encryption methods only accept PUCHAR's... (why?)
I found plenty of solutions but at first they dint seem to work. The first 128 characters from the ASCII table worked fine, but other characers like 'ù' and 'µ' were converted to other ones (mostly weird ASCII symbols, but always the same symbol for the corresponding given character).
I now just found out that the cast DOES work, but only for strings that are read from the console using cin?! Hardcoded strings do not work?! I honestly dont have a single clue about the cause of this behaviour.
Here is an example:
With CIN
cout << "With cin: ";
string password;
cin >> password;
unsigned char q = (unsigned char)password[0];
PUCHAR pbPassword = new unsigned char[1];
pbPassword[0] = q;
pbPassword[1] = NULL; //Null or garbage is printed
cout << pbPassword;
This outputs:
With cin:
µ
µ
Without CIN
cout << "Without cin: ";
string password = "µ";
unsigned char q = (unsigned char)password[0];
PUCHAR pbPassword = new unsigned char[1];
pbPassword[0] = q;
pbPassword[1] = NULL;
cout << pbPassword;
This outputs:
Without cin: ╡
I'm a beginning programmer so sorry if the code is messy.
Although i use the same character, the cast for the hardcoded string does not work. Even when using the exact same cast.
What i also noticed is that i can put a character at index 1, while the array only has a length of 1, meaning that i am accessing memory i actually shouldn't. How is this possible? Usually this gives a memory access error of some sorts right?
EDIT: The main question is not how to cast, or why i can still put elements in the array even if it has length 1. Its why cout gives different results for a cast from a string read from cin and a hardcoded string.