Почему никто не использовал sprintf?
#include <string>
#include <stdio.h>
static const std::string str = "hello world!";
int main()
{
//copy the data from the string to a char array
char *strarr = new char[str.size()+1];
strarr[str.size()+1] = 0; //set the null terminator
memcpy(strarr, str.c_str(),str.size()); //memory copy to the char array
printf(strarr);
printf("\n\nHEX: ");
//now print the data
for(int i = 0; i < str.size()+1; i++)
{
char x = strarr[i];
sprintf("%x ", reinterpret_cast<const char*>(x));
}
//DO NOT FORGET TO DELETE
delete(strarr);
return 0;
}