tags:

views:

261

answers:

3

I am building a string of int values, stored in a wchar_t*. If I have an integer, how can I append it onto the end of a wchar_t*? Windows only solutions are fine for this and I'd rather not include boost :)

+5  A: 

Use a wide version of stringstream and the '<<' operator. The correct operator to perform the conversion for you should be defined.

If I am missing some subtlety here you could depend on boost and use this.

I'm still a fan of secure versions of sprintf and so is Herb Sutter :D.

Hassan Syed
+1 for good answer.
Greg D
stringstreambuffer? can you possibly give an example?
Mark
sorry, its actually called a stringstream. it works like std::cout. You pump data into it -- like cout, and at the end you can retrieve a string from the stringstream. You can find a reference from www.cplusplus.com. If this doesn't help than I will post a more detailed example, please advise.
Hassan Syed
Example: `std::wstringstream ss; ss << 1; ss << 2; std::string s = ss.str();`
Pavel Minaev
yep that is it, thanks pavel. Was too drunk to think last night :P
Hassan Syed
Thanks a lot. However, after that cast I'm still not sure how you could append the two... I'll do some research. Thanks
Mark
Ok let me see if I can clarify things for you. StringStream is a stream that you can stream data into, just like cout. stringstream dynamically builds up a string from the input you supply -- growing the underlying buffer as needed. It keeps a ptr to the head of the string and all data gets added there. So to supply hello world as distinct words it would be: `ss << "Hello" << " " << "World" << "!" << "\n";` or you could break down the operations like pavel did above.
Hassan Syed
+3  A: 

If you are using windows you can always use wsprintf ie

wsprintf( newStr, L"%s%d", oldStr, yourInt );

I'm sure there will be some equivalent for non-windows ...

Goz
I allready used that as an answer, + suggesting sprintf before managed objects is not good when the tags imply c++ and newbie. Plus you have not picked the secure version of the function, (-1) for this reason. –
Hassan Syed
Pah ... nowt wrong with using a C solution to a C++ question. Half the reason a lot of C++ programmers wrie such awful code is this perceived need to only use OO constructs. OO is a useful tool but ISN'T the be all and end all ... BRING ON THE DOWNVOTES >:D
Goz
So this makes a brand new string? is it specific ot a certain type of string?
Mark
A: 

How about boost lexical_cast<>

std::wstring  data;

data += boost::lexical_cast<std::wstring>(53);
data.c_str() // This is wchar_t*
Martin York
already gave that answer.
Hassan Syed