Advertisement

Char to Float conversion

Started by March 23, 2000 10:41 AM
5 comments, last by C++ Freak 24 years, 9 months ago
Is there a function that takes a char and changes it to a float? Visit http://members.xoom.com/ivanickgames
Visit http://members.xoom.com/ivanickgames

Yep. Look for atof(const char* string)

example:

char* fltString = "1.4354";
float flt = atof(fltString);


Andrew





Advertisement
How would you reverse that? (float to string)

Is there a better way to record a float to a save file without using a structure? (I''m making a dynamically built save file)

E:cb woof!
E:cb woof!
quote: Original post by dog135

How would you reverse that? (float to string)


I don''t think that there is a built in function for that. To do this I usually do something like:

char fltBuffer[80];
float flt = 1.2345;
sprintf(fltBuffer, "%f", flt);

This will place the value inside the buffer string. You can also tell how many decimal places to keep and stuff like that.



quote:
Is there a better way to record a float to a save file without using a structure? (I''m making a dynamically built save file)

E:cb woof!


I am not sure what you mean here. To save a float to a file I use:

FILE* fileStream = fopen("test", "wb");
float flt = 1.2345;
fwrite(&flt, sizeof(flt), 1, fileStream);
fclose(fileStream);

I don''t know if this is what you mean.


Andrew
Just a follow-up here after looking in the help.

There is a function that will convert a float to a string (at least under VC++ 5).

Try looking at the _ecvt() function. I haven''t used it personally but it may do the trick.


Andrew
Thanks for all your help.

Visit http://members.xoom.com/ivanickgames
Visit http://members.xoom.com/ivanickgames
Advertisement
I once did a basic program in my course that decypted file where the characters are saved as integers. All that was nessary to convert the integers into chars and back again was to cast the datatype.
eg. char character = (char)floatnum;

hope that helps.

This topic is closed to new replies.

Advertisement