Archived

This topic is now archived and is closed to further replies.

Integer to String

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

How can i turn an integer into a string? Like if int blah = 5, i want to be able to stick that 5 into a string and not turn it into a char. Anyone understand what im saying and know how to do this?

Share this post


Link to post
Share on other sites
quote:
Original post by Mathematix
This is not a game, so it must be homework.

How do you figure? I haven''t played many games that didn''t use strings in one form or another.

Share this post


Link to post
Share on other sites
quote:
Original post by JoHnOnIzEr
I need to pass it to a function though as a char ><. And mathematix, i dont take a programming course


Again, google. I can''t see it taking you more than 5 straight seconds to find an answer to this.

Look up:
string::c_str()

Share this post


Link to post
Share on other sites

// needed stl headers
#pragma warning(disable:4786)
#include <sstream>
#include <string>

// variables
std::stringstream ss;
std::string MyString;
int blah = 5;

// convert from int to string
ss << blah;
ss >> MyString;

// get char* array
char const* pszMyString = MyString.c_str();

Share this post


Link to post
Share on other sites
quote:
Original post by glassJAw
int blah = 5;
string s;
s = blah;



This doesn''t do what you think it does. This creates a string where the first character is equal to a non-printing ascii character. s will not equal "5".

Share this post


Link to post
Share on other sites
whoa guys. How about itoa()?

Here''s the prototype:
char * itoa ( int value, char * buffer, int radix );

radix is the base number system you want to display it in. 99% of the time, it''s 10.

More info here.

Share this post


Link to post
Share on other sites
Problem with itoa() is it not available on many platforms. Most platforms just assume you''re going to use sprintf() if it''s a C based platform or the newer boost::lexical_cast or std::ostringstream if you''re using the c++ method. Herb Sutter has written a very good comparison article between all the string formatters of the realm and you can find the article here

Share this post


Link to post
Share on other sites
quote:
Original post by Beer Hunter
quote:
Original post by Odoacer
whoa guys. How about itoa()?
Nonstandard.


Isn''t stdlib.h standard? If so, I would think it would be available on all platforms...?

If not, oh well. I learned something today - don''t trust cplusplus.com!

Share this post


Link to post
Share on other sites
quote:
Original post by SiCrane
quote:
Original post by glassJAw
int blah = 5;
string s;
s = blah;



This doesn''t do what you think it does. This creates a string where the first character is equal to a non-printing ascii character. s will not equal "5".


Whoops. My mistake.

I guess a stringstream is needed then.

int blah = 5;
stringstream ss;
string s;

ss << blah;
s = ss.str(); //or ''ss >> s;''

Share this post


Link to post
Share on other sites
quote:
Original post by SiCrane
No, the lesson is to read the entire entry. It says specifically in the Portability section that itoa() is not ANSI.


Whoops. Yeah, didn''t see that. Heh

Share this post


Link to post
Share on other sites
cplusplus.com tends to have examples/information that predates the 98 C++ Standard. like for instance they use the non-templated versions of the headers (which won''t work in VC++ 2003 btw). So just be weary of it from that point of view.

Share this post


Link to post
Share on other sites
You guys work too hard:


#include <stdio.h> // for sprintf()
#include <iostream.h>

int main(int argc, char**argv)
{
int my_int = 13;
float my_float = 12.3;
char buf[25];
sprintf(buf, "int = %i\n float = %f\n", my_int, my_float);
cout << buf << endl;
}


Cheers
HTH



[edited by - countermind on January 4, 2004 2:46:30 AM]

Share this post


Link to post
Share on other sites
Main problem with your approach is type safety. There are a host of other problems with sprintf() that is talked about in the herb sutter article quoted above. boost::lexical_cast provides a nice solution:


int my_int = 13;
float my_float 12.3f;

std::string mystring = boost::lexical_cast<std::string>(my_int);
mystring += boost::lexical_cast<std::string>(my_float);

I'm not taking care of the exceptions that might be raised there either. Another thing I personally prefer the sprintf() style of syntax for anything more complex than type conversion over a stringstream but obviously sprintf() with all its hosts of problem just isn't an option but another boost library boost::format() provides sprintf() like functionality without the problems.

quote:
Original post by countermind
You guys work too hard:


#include <stdio.h> // for sprintf()
#include <iostream.h>

int main(int argc, char**argv)
{
int my_int = 13;
float my_float = 12.3;
char buf[25];
sprintf(buf, "int = %i\n float = %f\n", my_int, my_float);
cout << buf << endl;
}


Cheers
HTH
[edited by - countermind on January 4, 2004 2:46:30 AM]



[edited by - deepdene on January 4, 2004 12:15:46 PM]

Share this post


Link to post
Share on other sites
Yes, you should use a C++ string stream, as many people have mentioned. However, people have only mentioned the string streams whose character types are of type char, which is not a very generic/reusable solution. boost::lexical_cast, on the other hand, uses simple template metaprogramming to use the correct template arguments to instantiate std::basic_stringstream with, which makes it the best reusable solution.

#include <iostream>
#include <string>
#include <boost/lexical_cast.hpp>
extern int get_score();

...
{
   ...

   int score = get_score();
   std::string out = "Your score: " + boost::lexical_cast<std::string>(score);
   SomeAPIOutputFunction(..., out);

   int other_score = get_score();
   std::wstring new_out = L"Score: " + boost::lexical_cast<std::wstring>(other_score);
   std::wcout << new_out << std::endl;

   ...
}


[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on January 4, 2004 10:32:30 AM]

Share this post


Link to post
Share on other sites