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
Try this:
With appropriate includes:
Something like this:
Copy the result from:
Note that the result of
c_str
is a temporary(!)const char*
so if your function takeschar *
you will need to allocate a mutable copy somewhere. (Perhaps copy it to astd::vector<char>
.)str.c_str()
will return a null-terminated C-string.Note: not answering the main question since your comment indicated it wasn't necessary.
I've used sprintf to format your number as a hexadecimal.
those who write "const char* myString = "a number";" are just lousy programmers. Being not able to get the C basics - they rush into C++ and start speaking about the things they just don't understand.
"const char *" type is a pointer. "a number" - is array. You mix pointers and arrays. Yes, C++ compilers sometimes allow duct typing. But you must also understand - if you do duct typing not understanding where your "ductivity" is - all your program is just a duct tape.