| | |
Cast double to const char*
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2007
Posts: 87
Reputation:
Solved Threads: 4
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.
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.
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.
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.
>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.
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.
Here's an interesting discussion on the topic:
http://www.codeproject.com/string/st...onversions.asp
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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
![]() |
Similar Threads
- first cannot conver std::string to const char, now runtime error! (C++)
- How to convert string to const char* in C (C++)
- Converting Int to Const Char (C)
- Problems casting a const char* to char* (C++)
- AnsiString to Const Char* (C)
Other Threads in the C++ Forum
- Previous Thread: can anyone help me solving this program ??plz
- Next Thread: Need insight in assignment
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings struct temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






