I need to convert an ASCII string like… “hello2” into it’s decimal and or hexadecimal representation (a numeric form, the specific kind is irrelevant). So, “hello” would be : 68 65 6c 6c 6f 32 in HEX. How do I do this in C++ without just using a giant if statement?
EDIT: Okay so this is the solution I went with:
int main()
{
string c = "B";
char *cs = new char[c.size() + 1];
std::strcpy ( cs, c.c_str() );
cout << cs << endl;
char a = *cs;
int as = a;
cout << as << endl;
return 0;
}
You can use printf() to write the result to stdout or you could use sprintf / snprintf to write the result to a string. The key here is the %X in the format string.
If dealing with a C++ std::string, you could use the string’s c_str() method to yield a C character array.