Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > double to char*, how?

double to char*, how?
Thread Tools
Dedicated MacNNer
Join Date: May 2002
Location: Brooklyn, NY
Status: Offline
Reply With Quote
Jan 9, 2003, 11:19 PM
 
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
Reply With Quote
Jan 10, 2003, 01:00 AM
 
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
Reply With Quote
Jan 10, 2003, 01:23 AM
 
easier still:

char mystring[1024];
float mynum = 10;

sprintf(mystring, "%f", mynum); //throws mynum into mystring
     
Zimwy  (op)
Dedicated MacNNer
Join Date: May 2002
Location: Brooklyn, NY
Status: Offline
Reply With Quote
Jan 11, 2003, 12:02 PM
 
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
Reply With Quote
Jan 11, 2003, 01:04 PM
 
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
Reply With Quote
Jan 11, 2003, 10:17 PM
 
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
Reply With Quote
Jan 12, 2003, 04:24 AM
 
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.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 02:02 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2