 |
 |
double to char*, how?
|
 |
|
 |
|
Dedicated MacNNer
Join Date: May 2002
Location: Brooklyn, NY
Status:
Offline
|
|
Hey,
I was wondering if anyone could help me turning a double, float, or int, into a char* string? Thanks!!
Gabe
|
|
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Oct 2001
Status:
Offline
|
|
Code:
char* str(const char* fmt, ...)
{
char msg[1024];
va_list args;
va_start(args, fmt);
vsprintf(msg, fmt, args);
va_end(args);
return msg;
}
|
|
I be that insane n***a from the psycho ward.
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Jan 2001
Status:
Offline
|
|
easier still:
char mystring[1024];
float mynum = 10;
sprintf(mystring, "%f", mynum); //throws mynum into mystring
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: May 2002
Location: Brooklyn, NY
Status:
Offline
|
|
Cool! Thanks,
Where can I find out what the heck sprintf() does? It's nice that it works, but I'd like to be able to at least pretend I know what's going on. Thanks again!!
Gabe
|
|
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Oct 2001
Status:
Offline
|
|
In the terminal type "man sprintf"
type R
|
|
I be that insane n***a from the psycho ward.
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Aug 2002
Location: Northeastern NV, USA
Status:
Offline
|
|
Originally posted by Zimwy:
Cool! Thanks,
Where can I find out what the heck sprintf() does? It's nice that it works, but I'd like to be able to at least pretend I know what's going on. Thanks again!!
Gabe
sprintf() works just like printf() and fprintf()...
printf() sends an output string to the screen (usually stdout).
fprintf() works just like printf() but instead of sending an output string to the screen it sends it to a file.
sprintf() also works just like printf(), but in this case it sends the output string to a different string.
Grab any beginning C programming book and it will explain it all clearly. If you are not familiar with the C programming language then I suggest you don't look in the man page for sprintf() for it will most likely confuse the geebers out of you.
Thanks!
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status:
Offline
|
|
Just for fun, here's another way. This is a fairly "heavy" method, but it has the advantage of being heavily customizable.
Code:
#include <sstream>
#include <iomanip>
void MyConvertDouble
(const double& myDouble) {
char *finalString;
std::stringstream tempStream;
float outputFloat;
tempStream << myDouble;
// get a 'c-string' version of our string-stream
finalString = tempStream.str();
std::cout << finalString << std::endl;
// output our string-stream back to a float
tempStream >> noskipws >> outputFloat;
if (! tempStream) {
// there was an error
}
}
|
Geekspiff - generating spiffdiddlee software since before you began paying attention.
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|