Jump to content
  • Advertisement
Sign in to follow this  
Raeldor

C++ Converting Numerics to String for Display

This topic is 4825 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!