Sign in to follow this  
Tertsi

Any convenient ways of converting ints to strings?

Recommended Posts

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
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
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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this