Jump to content
  • Advertisement
Sign in to follow this  
chbrules

[C++]Int to Char

This topic is 4699 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

I have an integer value, but I want it to be shown as a char. I don't mean ASCII values, I mean that if the int is equal to say, 128, I want the values 128 shown as chars. String class is applicable here too, probably would be easier for me? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
with c++

do this

#include <sstream>
#include <string>

using namespace std;

string to_string( int val )
{
stringstream stream;
stream << val;
return stream.str();
}

// or more generally

template< class Type >
string to_string( Type val )
{
stringstream stream;
stream << val;
return stream.str();
}


i learned this yesterday, so i may be *very* wrong

Share this post


Link to post
Share on other sites
rip-off is correct. Use stringstreams. In my opinion, they are the easiest way to do this. They're a great thing to have in your toolbox.

Share this post


Link to post
Share on other sites
Wasn't there a thread recently in which someone explained why not to use stringstreams? (bad performance, memory leaks etc.) Anyway, I've seen a benchmark somewhere (not sure where) in which itoa() was about 6 times as fast as std::stringstream.

Share this post


Link to post
Share on other sites
Quote:
Original post by Kalasjniekof
Wasn't there a thread recently in which someone explained why not to use stringstreams? (bad performance, memory leaks etc.) Anyway, I've seen a benchmark somewhere (not sure where) in which itoa() was about 6 times as fast as std::stringstream.


There's something seriously, seriously wrong with your program if converting from a string to an int is a bottleneck. Speed is irrelevant. Memory leaks would be surprising, unless you forget to delete the characters you pass to it.

Share this post


Link to post
Share on other sites
Sorry for such a dumb question, I completely forgot about all the conversion functions, getting back into the C++ groove. ;)

Thanks guys

Share this post


Link to post
Share on other sites
Quote:
Original post by parklife
How about the old trusty sprintf? It's what I use most of the time.


you misspelled snprintf there

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
Quote:
Original post by parklife
How about the old trusty sprintf? It's what I use most of the time.


you misspelled snprintf there

There's pretty much no reason to use snprintf in this situation.

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!