7

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
user69514
  • 26,935
  • 59
  • 154
  • 188
  • Note: I don't need to print it in that format. I can do that. I have a function that takes a char* as a parameter. I need to convert it so I can use it for that function – user69514 Nov 19 '09 at 20:05

5 Answers5

20

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

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
4

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());
Mike Marshall
  • 7,788
  • 4
  • 39
  • 63
  • Start with a `std::string`, not a `const char *`. – David Thornley Nov 19 '09 at 20:10
  • 3
    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. – Mike Marshall Nov 19 '09 at 20:18
2
#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
  • 34,255
  • 14
  • 110
  • 165
  • Requirements were to separate by a tab, not a space. – David Thornley Nov 19 '09 at 20:10
  • I don't think that warrants a downvote since the hardest part isn't including a tab :-/ – Jacob Nov 19 '09 at 20:11
  • 1) sprintf_s is not standard - 2) this is the C way to do that – KeatsPeeks Nov 19 '09 at 20:11
  • 1
    Well, the OP wants to use a `char*`, hence `sprintf` isn't *so* unwarranted! – Jacob Nov 19 '09 at 20:17
  • 3
    Jacob's updated the answer to use `sprintf`. And since when were using C library functions illegal in C++ code? – Michael Kristofik Nov 19 '09 at 20:18
  • 1
    @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 Nov 19 '09 at 20:27
  • 2
    Yes, using C functions in C++ is often a lot easier. Regardless as to whether its "good practice" or not... whatever that means. – jheriko Nov 19 '09 at 20:29
  • 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 ? – KeatsPeeks Nov 19 '09 at 21:29
  • -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 Nov 19 '09 at 23:19
  • I would expect the OP to adjust the buffer length to his own needs. Or dynamically calculate it - which is probably overkill. – Jacob Nov 20 '09 at 00:02
1

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
  • 12,861
  • 8
  • 51
  • 80
0

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.