Sign in to follow this  

C++ Converting Numerics to String for Display

This topic is 4614 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, What is the best way of converting numeric values to strings for display output using graphics.drawstring, or some other similar function? Thanks

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You should google for std::stringstream

Share this post


Link to post
Share on other sites
The hardcore C++ way is std::stringstream or boost::format (which uses std::stringstream). The efficient C way is to use sprintf(). (You can also take a look at itoa and related functions, but these are not standard.)

Share this post


Link to post
Share on other sites
Well, simplest way probably is to use sprintf. It works exactly like printf or fprintf. But it's also the best way to create some nasty buffer overflow.
With std:stringstream you have type safety and with std::string you won't get buffer owerflows.
You can use it like this:


#include <sstream>

using namespace std;

stringstream Stream;
int x = 10;
float y = 20.5f;

Stream << "Int=" << x << " Float=" << y;

cout << Stream.str() << endl;

Share this post


Link to post
Share on other sites
Quote:
Original post by b2b3
Well, simplest way probably is to use sprintf. It works exactly like printf or fprintf. But it's also the best way to create some nasty buffer overflow.
With std:stringstream you have type safety and with std::string you won't get buffer owerflows.
You can use it like this:


#include <sstream>

using namespace std;

stringstream Stream;
int x = 10;
float y = 20.5f;

Stream << "Int=" << x << " Float=" << y;

cout << Stream.str() << endl;


And is it easy to convert a string stream into a null terminated character or wide-character string?

Share this post


Link to post
Share on other sites
To convert std::string to char* there's method std::string::c_str() which returns pointer to null terminated char array which contains your string. You should not modify data in returned array nor delete it. You just use it and forget about it [smile].

Example:

std::string s("Hello universe.");

printf("%s\n", s.c_str());


More info at MSDN.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
The hardcore C++ way is std::stringstream or boost::format (which uses std::stringstream). The efficient C way is to use sprintf(). (You can also take a look at itoa and related functions, but these are not standard.)

In theory std::stringstream could be implemented more efficiently than sprintf(), though in practice it often is not. I think more accurate would be 'the dangerous, prone to disaster, C way is to use sprintf()'...

Share this post


Link to post
Share on other sites
Ok, so I now have...

stringstream sMeshpos;
int x=10, y=20;
sMeshpos << "position=" << x << "," << y;
g.DrawString(sMeshpos.str().c_str(), -1, &font, pointF, &solidBrush);

however, it seems drawstring takes a widecharacter argument. Is there a c_str() function that will convert to wide?

Thanks

Share this post


Link to post
Share on other sites

// using namespace std is a bad habit. Explict detail is worth a few extra keystrokes
std::wstringstream sMeshpos; // note the 'w' on the beginning
int x=10, y=20;
sMeshpos << L"position=" << x << L"," << y; // note the 'L' for the Long strings
g.DrawString(sMeshpos.str().c_str(), -1, &font, pointF, &solidBrush);


That should do it.

Share this post


Link to post
Share on other sites
Quote:
Original post by mstorer

// using namespace std is a bad habit. Explict detail is worth a few extra keystrokes
std::wstringstream sMeshpos; // note the 'w' on the beginning
int x=10, y=20;
sMeshpos << L"position=" << x << L"," << y; // note the 'L' for the Long strings
g.DrawString(sMeshpos.str().c_str(), -1, &font, pointF, &solidBrush);


That should do it.


Awesome, thanks a lot guys that worked like a charm. I have been putting off delving into the standard library and the stl, but looks like I can put it off no longer!

Share this post


Link to post
Share on other sites
Sign in to follow this