Board index » cppbuilder » Converting numbers to Strings and viceversa.
Daniel Baylon
CBuilder Developer |
Daniel Baylon
CBuilder Developer |
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. |
Ed Mulroy [TeamB]
CBuilder Developer |
2007-07-26 06:43:23 AM
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 QuoteDaniel Baylon wrote in message |
Alan Bellingham
CBuilder Developer |
2007-07-26 07:08:55 AM
Re:Converting numbers to Strings and viceversa.
"Daniel Baylon" < XXXX@XXXXX.COM >wrote:
QuoteI'm newbie at C++, 'm trying to write a code which converts numbers (int, float, double) to strings (String) using standard C++ libraries. 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} |
Asger Jorgensen
CBuilder Developer |
2007-07-26 10:21:27 AM
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 |
Alan Bellingham
CBuilder Developer |
2007-07-26 05:25:39 PM
Re:Converting numbers to Strings and viceversa.
Asger Jorgensen < XXXX@XXXXX.COM >wrote:
QuoteYou aske for the C++ way and you got the C++ way from advisable to use the C++ idioms as being more safe.) Quoteand the other way: is>>doubleVal; Or boost::lexical_cast<double>(strVal); Alan Bellingham -- Team Browns ACCU Conference 2008: 2-5 April 2008 - Oxford (probably), UK |
Asger Jorgensen
CBuilder Developer |
2007-07-26 09:55:32 PM
Re:Converting numbers to Strings and viceversa.
In article < XXXX@XXXXX.COM >, XXXX@XXXXX.COM
says... Quote>sprintf(buffer, "%d", int_variable); 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 |
Chris Uzdavinis
CBuilder Developer |
2007-07-26 11:29:37 PM
Re:Converting numbers to Strings and viceversa.
Asger Jorgensen < XXXX@XXXXX.COM >writes:
QuoteI don't know how much newbie Daniel is, but I sure remember It really depends on whether you #include <stdio.h>or <cstdio>. -- Chris (TeamB); |
maeder
CBuilder Developer |
2007-07-27 01:38:54 AM
Re:Converting numbers to Strings and viceversa.
Asger Jorgensen < XXXX@XXXXX.COM >writes:
Quotedouble Val = atof("12345.78"); |