Sign in to follow this  
HarrisonPloeg

converting from char to float and float to char

Recommended Posts

HarrisonPloeg    100
How do you convert from char to float so that if the char array has 2.02 stored in it I can make the float variable = 2.02? [Edited by - HarrisonPloeg on March 6, 2007 3:33:56 AM]

Share this post


Link to post
Share on other sites
faa    151
hii..
To convert from float to char use the function fcvt
ex.
char *fps=NULL;
int dec,sign;
float LastFrameRate = 12.33f

fps=_fcvt(LastFrameRate,2,&dec,&sign);

The number 2 specifies the number of digits after the decimal point.dont worry about the last 2 parameters..

just in case u need to convert from integer to char..its itoa and from char to integer its atoi...

hope this helps...

Share this post


Link to post
Share on other sites
Aardvajk    13207
Unless you are using C++, in which case use strings and stringstreams:


#include <sstream>

float str_to_float(const std::string &in) // you can still pass a const char* to this
{
std::ostringstream o(in); float f; o >> f; return f;
}

std::string float_to_str(float f)
{
std::istringstream i; i << f; return i.str();
}


Or you could not reinvent this particular wheel, and use boost::lexical_cast.

Share this post


Link to post
Share on other sites
Zahlman    1682
First tell me what you think a char is. Note that a char is not the same thing as an array of char. Also, an array of char is not "a string", in a useful sense - if you're thinking of strings, you should be using real strings.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this