• Advertisement
Sign in to follow this  

Any convenient ways of converting ints to strings?

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

Are there any convenient ways of converting ints to strings? Without having to do huge if ladders or alike.

Share this post


Link to post
Share on other sites
Advertisement
Boost lexical_cast, or for a basic mockup of what it does:

template<typename R, typename T>
R lexical_cast(const T& t) {
R ret;
std::stringstream st;
if(!(st<<t) || !(st>>ret))
throw std::bad_cast();
return ret;
}

//usage:
std::string s = lexical_cast<std::string>(10);

Share this post


Link to post
Share on other sites
The way I do it:

inline std::string IntToString(int nNumber)
{
char szBuff[32];

sprintf(szBuff,"%d",nNumber);
return szBuff;
}

Share this post


Link to post
Share on other sites
Thanks, after reading the resource page I went for a similar approach than that of Evil Steves'. :)

Share this post


Link to post
Share on other sites
I normally use itoa, but after seeing the way you do it Evil Steve, I think I'm going to change my ways ;-)
What are the params for it though?

Edit: Do you have a link that tells me how to use this fully? I've had a look on msdn, and it's confusing and not very helpful (doesn't give a list of possible params)

Share this post


Link to post
Share on other sites
eh, I'm not sure what you're asking Tera, but the Evil's function is ready for usage.

ex.
int month = 10;
string tempmonth;
tempmonth = IntToString(month);

Share this post


Link to post
Share on other sites
Ok, maybe I didn't word that well.
What I wanted to know was what does the "%d" mean? And what other flags are there?

Share this post


Link to post
Share on other sites

void int2string(int n, std::string &str)
{
do
{
int d = n % 10;
n = n / 10;

str += '0' + d;
} while(n != 0);
}


That should do it.

Share this post


Link to post
Share on other sites
Quote:
Original post by Tera_Dragon
Ok, maybe I didn't word that well.
What I wanted to know was what does the "%d" mean? And what other flags are there?


%d - insert int
%f - insert float
%x - insert int in hex form
%s - insert string

there are some more but i cant remember them

Share this post


Link to post
Share on other sites
%d reads a decimal integer
%i reads an integer
%c reads a char
%f reads a float
%s reads a string
%u reads an unsigned int
etc.

You should find the rest of the flags easily by searching for sprintf reference.

edit: damn mike, was just typing. ;P

Share this post


Link to post
Share on other sites
Quote:
Original post by Tera_Dragon
I normally use itoa, but after seeing the way you do it Evil Steve, I think I'm going to change my ways ;-)

Yeah, I used to use itoa() also. But its MSVC only, and I needed my code to work with GCC on Linux also. sprintf() is ANSI C

Share this post


Link to post
Share on other sites
I'm with Washu on the std::stringstream or boost:lexical_cast methods, but hey, if straight C is your thing, I suppose sprintf works well enough.

Share this post


Link to post
Share on other sites
macros to the rescue to cover all your bases for conversion

#define NUMTOSTRING(x,y) std::string NumToString(x valueIn) { char NumberOut[32]; sprintf(NumberOut, "%" #y, valueIn); return NumberOut; };

NUMTOSTRING(unsigned __int32, lu);
NUMTOSTRING(unsigned __int16, u);
NUMTOSTRING(unsigned __int8, hu);
NUMTOSTRING(__int32, ld);
NUMTOSTRING(__int16, d);
NUMTOSTRING(__int8, hd);
NUMTOSTRING(float, f);
//and so on


//in main
unsigned __int32 test1 = 54;
unsigned __int16 test2 = 435;
unsigned __int8 test3 = 32;
__int32 test4 = -34444;
__int16 test5 = -334;
__int8 test6 = -13;
float test7=110.324f;

printf("%s \n", NumToString(test1).c_str());
printf("%s \n", NumToString(test2).c_str());
printf("%s \n", NumToString(test3).c_str());
printf("%s \n", NumToString(test4).c_str());
printf("%s \n", NumToString(test5).c_str());
printf("%s \n", NumToString(test6).c_str());
printf("%s \n", NumToString(test7).c_str());



Share this post


Link to post
Share on other sites
Use Washu's code. It's typesafe and obvious. And doesn't use macros. And will work for any type which supports operator<< (e.g. std::complex, your own types, etc)

Share this post


Link to post
Share on other sites
CoffeeMug: You forgot to take negative numbers and 0 into account. Otherwise that works great. You can easily add the radix as a parameter too, which makes it extra fun.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement