I am just wondering what is the most elegant and efficient way to do
such type conversion.
Thanks,
Flyingway 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
Hi Ivan,
That is so helpful! You are greatly appreciated. Thanks.
Best wishes,
Flying...
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
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
"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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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...
|
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
|
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
|
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
| |
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?
|
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?
...
|
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.
|
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...
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |