the stl way:
Main Topics
Browse All TopicsHi,
I have the following snippet of code (C based):
unsigned int val = 10;
char str[256];
sprintf(str, "Hex value: %x", 10);
I want to accomplish the same thing using C++ strings. How do I do this?
Thanks!
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
>> the stl way:
It becomes a lot easier if you make use of the <iomanip> standard library ;)
http://cplusplus.com/refer
to add to above solution:
You could spare the std:: prefix if adding a using directive.
When defining a stringstream for output only, you normally would use the ostringstream class.
If outputting characters as hex digits you normal would have always 2 digits, i. e. you need leading zeros for numbers from 0x00 to 0x09. You can do that by using the manipulators std::right, std::setfill, and std::setw.
All together gives code like
>> Thanks for all your comments! Infinity08, is the <strstream> library deprecated? Is <sstream> its replacement?
<strstream> is provided for compatibility reasons only, to use char* stream buffers.
For C++, prefer using <sstream>, since they provide the actual C++ string (std::string) streams.
>> You could spare the std:: prefix if adding a using directive.
But prefer not to do that, since it dumps the entire std namespace in the current (compilation unit) namespace, and that can cause unexpected problems.
>>>> and that can cause unexpected problems.
The usual recommendation is to have all headers without using clauses (i. e. specifying the std:: prefix for all STL member types and argument types) and add the using clause in the .cpp file below all include statements. If you do so, you rarely will encounter namespace issues. I didn't experience not one single case since namespaces were added to C++ in the 90-ties.
I have, and others have too. If you don't believe me, maybe this'll be an acceptable source :
http://www.parashift.com/c
(read it completely for even more reasons not to use the using directive, not even for the std namespace)
In any case, this is not the place to discuss this, since this question is about the C++ equivalent for the C code in the question. If you want, you know how to contact me.
I believe, the "using namespace std;" or not is not off-topic here as the replacement of sprintf should also be readable.
I was also a kind of "never let out std::". Anyway, readability counts! I agree with following the overall (company) style when the sources are managed by more people. Still, I can see no problem with
using namespace std;
AT THE BEGINNING OF THE .CPP FILES -- but never .h files!
Despite of warnings like "something will definitely be broken"... I have never observed a big or unnoticed problem in my sources. That is also because, the "using namespace std;" is local in the .cpp and I tend not to use the same identifiers as the STL does. The compiler cries "unambiguous!" if it is not clear. If anything needs to be clarified, the std:: can always be prepended to the ambiguous place.
It is off topic, because the question is not about readability, or coding standards, or anything related to namespaces. It is about finding the C++ equivalent for the posted C code.
Whatever you post here about using namespace std; or not is not likely to be useful in the PAQ database, since people who are looking for this kind of information will not find it.
So, since there seem to be several people who want to discuss this, I've opened a new question just for the purpose of discussing the (dis)advantages of using namespace std. Feel free to participate there, and consider keeping this question just about the original question for the sake of the PAQ database ;)
http://www.experts-exchang
Business Accounts
Answer for Membership
by: FarzadAPosted on 2009-08-11 at 21:25:41ID: 25075770
Hello,
Try the following code. I am using Microsoft Visual C++ 6.0 compiler and CString MFC Classes.
-FA
Select allOpen in new window