Jump to content
  • Advertisement
Sign in to follow this  
johnnyBravo

c++, converting number to string, best way?

This topic is 5409 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, I was wondering what the best way of converting a number(im using an integer in this case), to a string? I don't really want to use stringstream(for my own reasons). I've am using sprintf, but the visual c++ 2005 beta ide keeps telling me its depreciated in a warning debug message. So i tried the next function that was listed in the help file, but that was depreciated aswell, (_snprintf) anyway so what should i use? Thanks

Share this post


Link to post
Share on other sites
Advertisement
If it's just due to the effort of using a stringstream, why not try this little helper, which can be found in the boost library:

template<typename TargetType, typename SourceType>
inline TargetType lexical_cast(SourceType from) {
std::stringstream s;
s << from;

TargetType to;
s >> to;

return to;
}

int main() {
// string-to-int
int i = lexical_cast<int>("12345");
// int-to-string
string s = lexical_cast<string>(12345);
}


Perfectly matches the C++ conventions of static_cast<> or reinterpret_cast<> and can be used inline without temporary variables.

Otherwise, check our itoa() and atoi().

-Markus-

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
If you want to to keep modern, I believe this will work

std::strstream s;
s << "123456" << ends;
string str = s.str();

Mind you I like Cygon's solution, it's cleaner and more generic...

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
If you want to to keep modern, I believe this will work

std::strstream s;
If you want to stay modern, use std::stringstream. Std::strstream was deprecated in the first standard. [smile]

Share this post


Link to post
Share on other sites
Quote:
Original post by petewood
Quote:
Original post by johnnyBravo
I don't really want to use stringstream(for my own reasons).

which are?


just dont want to include the header :)

because these are directx classes like vertex buffer and index buffer, where i just dont really need any string functions , as i just want to convert numbers from error returns to string to output in the debug.

cuz thats the only reason, i didnt really want to add any extra headers

Share this post


Link to post
Share on other sites
Quote:
Original post by johnnyBravo
Quote:
Original post by petewood
Quote:
Original post by johnnyBravo
I don't really want to use stringstream(for my own reasons).

which are?


just dont want to include the header :)

because these are directx classes like vertex buffer and index buffer, where i just dont really need any string functions , as i just want to convert numbers from error returns to string to output in the debug.

If you're using the usual std::ostream for your debug output you don't need to convert it to a string first anyway; it will do the same kind of thing as stringstream for you.

If you're worried about headers, especially templated headers, which can take a long time to compile sometimes, I've found putting them into a pre-compiled header is a very effect way of speeding up compile times. The standard library headers, boost library headers and directx headers and anything else which isn't going to change, or not often, are just the thing you'd want to put in there.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!