C++ convert int and string to char*

c++, char, string

Solution

With appropriate includes:

#include <sstream>
#include <ostream>
#include <iomanip>

Something like this:

std::ostringstream oss;
oss << std::hex << a << '\t' << str << '\n';

Copy the result from:

oss.str().c_str()

Note that the result of `c_str` is a temporary(!) `const char*` so if your function takes `char *` you will need to allocate a mutable copy somewhere. (Perhaps copy it to a `std::vector<char>`.)

Problem

This is a little hard I can't figure it out. I have an int and a string that I need to store it as a char*, the int must be in hex i.e. ``` int a = 31; string str = "a number"; ``` I need to put both separate by a tab into a char*. Output should be like this: ``` 1F a number ```

Original source