Cast double to const char*

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 87
Reputation: prushik is on a distinguished road 
Solved Threads: 4
prushik prushik is offline Offline
Junior Poster in Training

Cast double to const char*

 
0
  #1
Oct 23rd, 2007
I would like to know once and for all how to convert a double to a const char*

I've tried it a few times, but have never succeeded, I know it can be done, but I haven't figured out how to yet. I've tried some different things. Sometimes it won't compile, and sometimes the program crashes, and other times it ends up as an empty string.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 48
Reputation: amt_muk is an unknown quantity at this point 
Solved Threads: 3
amt_muk amt_muk is offline Offline
Light Poster

Re: Cast double to const char*

 
0
  #2
Oct 23rd, 2007
U can try sprintf().

Amit
Last edited by amt_muk; Oct 23rd, 2007 at 4:13 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,362
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Cast double to const char*

 
0
  #3
Oct 23rd, 2007
depends on how you want to use it. If you want to pass a pointer to a double as a parameter to some function that takes char* then typecasting might work, depending on what that function is going to do with it. Normally, however, you will have to convert it storing the results in either a character array or a std::string object.

Two ways come to mind: sprintf() will convert and store in a character array, while std::stringstream will convert and store in std::string object.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 62
Reputation: Ptolemy is an unknown quantity at this point 
Solved Threads: 8
Ptolemy's Avatar
Ptolemy Ptolemy is offline Offline
Junior Poster in Training

Re: Cast double to const char*

 
0
  #4
Oct 23rd, 2007
>I would like to know once and for all how to convert a double to a const char*
Casting doesn't work. You're not converting the double to a string with a cast, you're telling the compiler to use the binary representation of the double as characters in a string. For a value like 12.35, that's not going to give you a string like "12.35". You need to take the printed value (like if you write the double using cout) and store it in a string. That's really really painful to do manually with floating-point, so you should use sprintf or stringstream like Ancient Dragon said.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,675
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Cast double to const char*

 
0
  #5
Oct 23rd, 2007
Here's an interesting discussion on the topic:
http://www.codeproject.com/string/st...onversions.asp
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 1
Reputation: Seta00 is an unknown quantity at this point 
Solved Threads: 0
Seta00 Seta00 is offline Offline
Newbie Poster

Re: Cast double to const char*

 
0
  #6
Nov 12th, 2008
http://www.cplusplus.com/reference/c...o/sprintf.html
____________________
Le Parkour LifeStyle
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC