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 :)
views:
261answers:
3
+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
2009-12-05 17:10:05
+1 for good answer.
Greg D
2009-12-05 17:12:38
stringstreambuffer? can you possibly give an example?
Mark
2009-12-05 17:56:26
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
2009-12-06 04:50:28
Example: `std::wstringstream ss; ss << 1; ss << 2; std::string s = ss.str();`
Pavel Minaev
2009-12-06 04:52:12
yep that is it, thanks pavel. Was too drunk to think last night :P
Hassan Syed
2009-12-06 14:41:07
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
2009-12-06 17:23:08
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
2009-12-07 00:22:44
+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
2009-12-05 17:18:19
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
2009-12-05 17:24:21
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
2009-12-05 17:34:10
So this makes a brand new string? is it specific ot a certain type of string?
Mark
2009-12-05 18:28:02
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
2009-12-05 17:25:13