generic string conv

i have very little exposure in c++,pl dont neglect my question.
i need  a generic function which accepts any primitive datatype and converts it into a string(STL) .
eg: string convString(<type eg:double,long,> value)

i have used _fcvt() for double to string(char *,which i converted later), but i found "." is missing in the string.

and i also need a function which will  find the length of primitive datatypes not sizeof(type);

eg: double d = 1234.25
int size = mySize(1234.25);
size should contain a value 7(includes ".", if it has);

eadha



eadhasAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
#define mySize(x) (sizeof #x -1)
0
basantCommented:
template <class T>
string ConvString( T a)
{
   strstream s;
   s << a;
return s.str();
}

This function will convert any C++
data type to a string but ..
This will change the size.

e.g ConvString( 2.3)

It may become 2.30000
and size will be 7 instead of
3 ( as u might expect).

But if u specify the correct
parameter for strstream s for
floating variables then u can
use it.

I think it is very difficult to
maintain the size e.g.

ConvString( +2.3)
You might expect the size 4 but
it will be 3 as + may miss.


But my function will convert
it to a string.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ozoCommented:
#define ConvString(x) #x
0
nietodCommented:
>> i need  a generic function which accepts any
>> primitive datatype and converts it
>> into a string(STL)
Use the stringstream class for this.  You can output information to the class with << just like any other stream.  Then you can obtain all the formatted information written to the class as a string.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.