how to directly convert char to numbers?

C++ how to convert a float to char?

  • How to convert a float to char in C++ : i wanna convert fVal = 65 to become char* cVal = "65" and NOT 'A' I tried itoa() but it converts only integers besides that ...show more

  • Answer:

    I got this problem When I was "developing" a small Direct3D application. Im using D3DXCreateFont in wich i must drop a LPCSTR variable that contains characters (i guess is a const char*). The problem is that i dont know exactly how to convert a float that represents a position of a light to a LPCSTR. Ive tryed to convert the float to char but when i use D3DXCreateFont function the program crashes here: ostream.h streamsize _Count = (streamsize)_Traits::length(_Val); // may overflow here is an example of std::stringstream: #include <string> #include <sstream> std::string float_to_str(float f) { std::stringstream stream; stream << f; return stream.str(); } int main() { std::cout << float_to_str(123.67898f) << std::endl; // if you need to use a LPCSTR, you have to use either // std::string some_string = float_to_str(some_float); // some_function_that_use_a_lpcstr(some_str... // or directly // some_function_that_use_a_lpcstr(float_to... } boost::lexical_cast<> works in a similar way. As a C++ guy that enjoys type safety and many other safety features of C++, I encourage you to avoid sprintf() (and any other *printf() function). Their C-style construction makes your code less safe and more bug prone. Anyway, a "somewhat correct" usage of sprintf would be: char buf[32]; // 32? 64? 128? who knows? that's the biggest problem // with sprintf: you must supply a buffer, buf you actually // don't know which size it is sprintf(buf, "%.2f", some_float); // %f is the format string for a float // the '.2' I used constrain sprintf to only print two decimals // you can have the same behavior with the stringstream by // using the stream manipulators. But again, I suggest you to use the std::stringstream version. I spoke about stream manipulators, here is how you constrain a stream to output only two decimals: std::stringstream stream; stream << std::setprecision(2) << f; This will guarantee that at most 2 digits after the decimal point will be displayed. If you want to force it to always display two decimals, you can insert std::fixed << between stream << and std::setprecision: stream << std::fixed << std::setprecision(2) << f; it really worked With me, wish the same to you.

46U2UFSB7MLWM6LWRRAAWAZ63Q at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

try sprintf("%f",chararray);

interview question and answer

u can't conv the float to char

pradyumnp

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.