473,791 Members | 3,105 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Convert a string to float and vice versa

I am just wondering what is the most elegant and efficient way to do
such type conversion.

Thanks,

Flyingway

Jul 23 '05 #1
6 4271
"Flyingaway " <hh*******@hotm ail.com> wrote in message
news:11******** **************@ l41g2000cwc.goo glegroups.com.. .
I am just wondering what is the most elegant and efficient way to do
such type conversion.

Consider using boost::lexical_ cast
See: http://www.boost.org/libs/conversion/lexical_cast.htm

For maximum efficiency, one could use a statically allocated
buffer and the functions atof (in <cstdlib>) and ftoa(when
available, otherwise sprintf). But for the latter you need
to be very careful with buffer overflows - using snprintf
may help...
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form


Jul 23 '05 #2
Hi Ivan,

That is so helpful! You are greatly appreciated. Thanks.

Best wishes,

Flying...

Jul 23 '05 #3
Stu
Ivan Vecerina wrote:
"Flyingaway " <hh*******@hotm ail.com> wrote in message
news:11******** **************@ l41g2000cwc.goo glegroups.com.. .
I am just wondering what is the most elegant and efficient way to do
such type conversion.

Consider using boost::lexical_ cast
See: http://www.boost.org/libs/conversion/lexical_cast.htm

For maximum efficiency, one could use a statically allocated
buffer and the functions atof (in <cstdlib>) and ftoa(when
available, otherwise sprintf). But for the latter you need
to be very careful with buffer overflows - using snprintf
may help...


Why not just use a stringstream? Something like:

#include <sstream>

....

std::stringstre am ss;

....

// Cast to float...
ss << some_string;
ss >> some_float;

....

// Cast to string...
ss >> some_float;
ss << some_string;

....
No third party library or C functions needed...
Stu
Jul 23 '05 #4
Stu
Stu wrote:
// Cast to string...
ss >> some_float;
ss << some_string;


Whoops, copy and paste error! It should look like this:

// Cast to string...
ss << some_float;
ss >> some_string;
Stu

Jul 23 '05 #5
"Stu" <st*@santa-li.com> wrote in message
news:0Z******** ************@co mcast.com...
Ivan Vecerina wrote:
"Flyingaway " <hh*******@hotm ail.com> wrote in message
news:11******** **************@ l41g2000cwc.goo glegroups.com.. .
I am just wondering what is the most elegant and efficient way to do
such type conversion. Consider using boost::lexical_ cast
See: http://www.boost.org/libs/conversion/lexical_cast.htm Seemed to be the most elegant.
For maximum efficiency, one could use a statically allocated
buffer and the functions atof (in <cstdlib>) and ftoa(when
available, otherwise sprintf). But for the latter you need
to be very careful with buffer overflows - using snprintf
may help...

That was for the most efficient.
Why not just use a stringstream? Something like:


I totally agree it's a good solution (and it is actually
boost::lexical_ cast uses by default). I had narrowly
answered the question that was posted...

Cheers,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Jul 23 '05 #6
On Sat, 19 Feb 2005 08:59:05 +0100, "Ivan Vecerina"
<IN************ *************@v ecerina.com> wrote in comp.lang.c++:
"Flyingaway " <hh*******@hotm ail.com> wrote in message
news:11******** **************@ l41g2000cwc.goo glegroups.com.. .
I am just wondering what is the most elegant and efficient way to do
such type conversion.

Consider using boost::lexical_ cast
See: http://www.boost.org/libs/conversion/lexical_cast.htm

For maximum efficiency, one could use a statically allocated
buffer and the functions atof (in <cstdlib>) and ftoa(when
available, otherwise sprintf). But for the latter you need
to be very careful with buffer overflows - using snprintf
may help...


Poor advice, atof and its siblings produce undefined behavior if the
converted value is outside the range of the type.

That is specifically why the strtod and siblings were invented.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
37230
by: Byron | last post by:
Hello, I am a newbie and would like to know if it is possible to convert a string back to a dictionary? For example, I can convert a dictionary to a string by doing this: >>> names = {"Candy" : 2.95, "Popcorn" : 4.95} >>> strNames = str(names) >>> print strNames
19
22739
by: Espen Ruud Schultz | last post by:
Lets say I have a char pointer and an std::string. Is it possible to get a pointer to the std::string's "content" so that the char pointer can point to the same text? And vice versa; can I give the std::string a pointer and a length and then give the std::string control over the pointer and its content? I'm basically trying to avoid copying large text between an std::string and a char pointer, and vice versa. Is there anyhing in the...
3
5702
by: Rodusa | last post by:
I am looking for an example of how to save a DataTable into Base64 and vice-versa. I tried using Convert.ToBase64String(), but it says that it cannot convert DataTable to byte. Any help or pointing would be appreciated. Thanks Rod
15
10836
by: Yifan | last post by:
Hi Does anybody know how to convert System::String* to char*? I searched the System::String class members and did not find any. Thanks Yifan
20
3447
by: Niyazi | last post by:
Hi all, I have a integer number from 1 to 37000. And I want to create a report in excel that shows in 4 alphanumeric length. Example: I can write the cutomerID from 1 to 9999 as: 1 ----> 0001 2 ----> 0002
4
17787
by: thinktwice | last post by:
i'm using VC++6 IDE i know i could use macros like A2T, T2A, but is there any way more decent way to do this?
6
39290
yabansu
by: yabansu | last post by:
Hi all, I think most of you probably know the two .NET framework functions, namely Encoding.GetBytes(string) and Encoding.GetString(byte), to convert string into byte array and vice versa. Now, I want to do the same thing in pure(unmanaged) C++. I searched the Internet but could not find any satisfactory solutions. I am really in need of help! Is there anyone to explain how I can implement these functions by not using .NET library? ...
0
1655
by: nudrat | last post by:
Hi, My requirement are : 1) In one PC in LAN Linus is installed which is used for net surfing.... rest of the systems have xp including exchange server. 2) We want to convert all email from linux (mbox) format to windows format (pst) and vice versa. Any body how can giude how to proceedstep by step for the same.
0
10785
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information inside an image, hide your complete image as text ,search for a particular image inside a directory, minimize the size of the image. However this is not a new concept, there is a concept called Steganography which enables to conceal your secret...
0
9669
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9515
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10427
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9029
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5431
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4110
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3718
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.