Board index » cppbuilder » Converting numbers to Strings and viceversa.

Converting numbers to Strings and viceversa.


2007-07-26 06:20:38 AM
cppbuilder93
I'm newbie at C++, 'm trying to write a code which converts numbers (int, float, double) to strings (String) using standard C++ libraries.
I haven't found functions or classes to do it.
I appreciate some help.
Regards.
 
 

Re:Converting numbers to Strings and viceversa.

You say you want to use standard C++ yet speak of "String". Start by
deciding if you really want standard C++ which has the "string" class or if
you are trying to use the VCL AnsiString whose typedef is "String".
Using standard C++
char buffer[128];
one of
std::sprintf(buffer, "%d", int_variable);
std::sprintf(buffer, "%f", float_variable);
std::sprintf(buffer, "%f", double_variable);
and then one of
string_variable = buffer;
string_variable = std::string(buffer);
. Ed
Quote
Daniel Baylon wrote in message
news:46a7ccb6$ XXXX@XXXXX.COM ...

I'm newbie at C++, 'm trying to write a code which converts numbers
(int, float, double) to strings (String) using standard C++ libraries.

I haven't found functions or classes to do it.

I appreciate some help.
 

Re:Converting numbers to Strings and viceversa.

"Daniel Baylon" < XXXX@XXXXX.COM >wrote:
Quote
I'm newbie at C++, 'm trying to write a code which converts numbers (int, float, double) to strings (String) using standard C++ libraries.

I haven't found functions or classes to do it.
std::ostringstream os;
os << val;
os.str(); <- a std::string
Note that val may be any of the types you mentioned. If you want it most
succinctly, go to www.boost.org, and get the boost::lexical_cast
library. It effectively wraps the above, but makes the code look like
boost::lexical_cast<std::string>(val);
Alan Bellingham
--
Team Browns
<url:www.borland.com/newsgroups/>Borland newsgroup descriptions
<url:www.borland.com/newsgroups/netiquette.html>netiquette
 

{smallsort}

Re:Converting numbers to Strings and viceversa.

Hi Daniel
You aske for the C++ way and you got the C++ way from
Ed and Alan.
But there is also the old C way:
sprintf(buffer, "%d", int_variable);
sprintf(buffer, "%f", float_variable);
sprintf(buffer, "%f", double_variable);
and the other way:
double Val = atof("12345.78");
int val = atoi("1234");
Kind regards
Asger
 

Re:Converting numbers to Strings and viceversa.

Asger Jorgensen < XXXX@XXXXX.COM >wrote:
Quote
You aske for the C++ way and you got the C++ way from
Ed and Alan.

But there is also the old C way:

sprintf(buffer, "%d", int_variable);
sprintf(buffer, "%f", float_variable);
sprintf(buffer, "%f", double_variable);
Which is the way that Ed showed, without the std:: qualification. (C
methods usually being usable as-is in C++, though it's often more
advisable to use the C++ idioms as being more safe.)
Quote
and the other way:

double Val = atof("12345.78");
int val = atoi("1234");
Oh, yes, the other way ... for string to double
std::istringstream is(strVal);
is>>doubleVal;
Or
boost::lexical_cast<double>(strVal);
Alan Bellingham
--
Team Browns
ACCU Conference 2008: 2-5 April 2008 - Oxford (probably), UK
 

Re:Converting numbers to Strings and viceversa.

In article < XXXX@XXXXX.COM >, XXXX@XXXXX.COM
says...
Quote
>sprintf(buffer, "%d", int_variable);
>sprintf(buffer, "%f", float_variable);
>sprintf(buffer, "%f", double_variable);

Which is the way that Ed showed, without the std:: qualification. (C
methods usually being usable as-is in C++, though it's often more
advisable to use the C++ idioms as being more safe.)
Of cource it is, I just copied his example and removed
the std:: ;-)
I don't know how much newbie Daniel is, but I sure remember
my self as a newbie where a simple std:: could confuse me
seriously, say I as if those days was over LOL.
Kind regards
Asger
 

Re:Converting numbers to Strings and viceversa.

Asger Jorgensen < XXXX@XXXXX.COM >writes:
Quote
I don't know how much newbie Daniel is, but I sure remember
my self as a newbie where a simple std:: could confuse me
seriously, say I as if those days was over LOL.
But if the code doesn't compile due to lack of "std" qualification,
that's probably even more confusing. :)
It really depends on whether you #include <stdio.h>or <cstdio>.
--
Chris (TeamB);
 

Re:Converting numbers to Strings and viceversa.

Asger Jorgensen < XXXX@XXXXX.COM >writes:
Quote
double Val = atof("12345.78");
int val = atoi("1234");
These two are not to be recommended, because they don't signal
conversion failures. Better use std::strtol() or std::strod() instead.