I am trying to take a user input as a string and convert each individual element into its hex equivalent as an unsigned char in C++. For example I want to take a user input "CL" and convert that into 0x43 and 0x4C stored in an unsigned char.
string ui;
unsinged char uihex;
cout << "Please enter in command to execute: ";
cin >> ui;
for (int i=0; i<ui.length()-1; i++)
{
uihex = static_cast<unsigned char>(ui);
}
If you want to print values in hexadecimal, you will need to cast the
char
variable to an integer type. This will help the compiler choose the correct function for printing: