Jump to content
  • Advertisement
Sign in to follow this  
HarrisonPloeg

converting from char to float and float to char

This topic is 4100 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!