[type] to string

Get answers to all your basic programming questions. No Ogre questions, please!
User avatar
paddy
Greenskin
Posts: 136
Joined: Sun Aug 01, 2004 7:07 pm

[type] to string

Post by paddy »

I am working mostly with CEGUI and strings right now, but I am having trouble figuring out the best way to get numbers and other datatypes into strings

I want to be able to do:

setStatusText("Setting the text here: "+ img->getHeight());

and have the function setup as

setStatusText(const CEGUI::String &txt) { ...}


I have tried lots of stuff but I just can't seem to find the right way to do this, its very basic I am sure, but I could use some help.

I am pretty sure if I can figure out the proper way to get it into a standard string I can go from that to CEGUIL::String but whatever is the most proper way is what I would like to use.
User avatar
temas
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 390
Joined: Sun Oct 06, 2002 11:19 pm
Location: The Woodlands, TX

Post by temas »

You'll probably want to use a string stream from <sstream>

An example:

Code: Select all

ostringstream os;

os << "Setting the text here: " << img->getHeight();

setStatusText(os.str());
User avatar
paddy
Greenskin
Posts: 136
Joined: Sun Aug 01, 2004 7:07 pm

Post by paddy »

works great thanks! :D
User avatar
epopov
Halfling
Posts: 85
Joined: Tue Jun 10, 2003 2:57 pm

Post by epopov »

boost (http://www.boost.org) also has some useful string util functions:

Code: Select all

setStatusText(boost::str(boost::format("Setting the text here:%d") % img->getHeight()));
It also works with more than one parameter:

Code: Select all

boost::str(boost::format("Height is %d and width %d") % img->getHeight() % img->getWidth()));