views:

645

answers:

5

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
+17  A: 

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>.)

Charles Bailey
+1. Great answer.
Kristo
only right answer here
Samuel_xL
this works awesome. thanks.
+1  A: 
#include <stdio.h>

char display_string[200];
sprintf(display_string,"%X\t%s",a,str.c_str());

I've used sprintf to format your number as a hexadecimal.

Jacob
Requirements were to separate by a tab, not a space.
David Thornley
I don't think that warrants a downvote since the hardest part isn't including a tab :-/
Jacob
1) sprintf_s is not standard - 2) this is the C way to do that
Samuel_xL
^ both warrant a downvote
Samuel_xL
Well, the OP wants to use a `char*`, hence `sprintf` isn't *so* unwarranted!
Jacob
Jacob's updated the answer to use `sprintf`. And since when were using C library functions illegal in C++ code?
Kristo
@Kristo: I agree, especially since I still find formatting using iostreams to be much more painful to read (or even get right in the first place). However, I think using `snprintf()` should be considered over `sprintf()`.
Michael Burr
Yes, using C functions in C++ is often a lot easier. Regardless as to whether its "good practice" or not... whatever that means.
jheriko
Just *don't teach bad practices to beginners*, ffs !
Samuel_xL
next time, tell everyone to not use "const" in fonction parameters, because "it's easier" and "it's not illegal". People like you are a real pain on SO. Every single day I'm reading such nonsense here. Won't you let the experts alone, instead of adding noise to this website ?
Samuel_xL
-1 for the buffer of length 200. It's either way too big (if we take the `str` given in the question), or not big enough (if `str` is of unknown length).
Steve Jessop
I would expect the OP to adjust the buffer length to his own needs. Or dynamically calculate it - which is probably overkill.
Jacob
+3  A: 

Try this:

int myInt = 31;
const char* myString = "a number";
std::string stdString = "a number";

char myString[100];

// from const char*
sprintf(myString, "%x\t%s", myInt, myString);

// from std::string   :)
sprintf(myString, "%x\t%s", myInt, stdString.c_str());
mjmarsh
Love to know why this got a downvote. Works great for me
mjmarsh
Start with a `std::string`, not a `const char *`.
David Thornley
C again............
Samuel_xL
Any modern C++ compiler that allows linking to the C-runtime will compile this code (Jacob's code is Windows-centric but still works on that platform). I've personally always loathed the overloaded << operators in the C++ I/O libraries, but I guess to each his own.
mjmarsh
+1  A: 

str.c_str() will return a null-terminated C-string.

Note: not answering the main question since your comment indicated it wasn't necessary.

int3
A: 

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.

Alexander Solonsky