none
Conversion from float to char[]

Answers

  • Well, yes, of course, but the OP didn't say how long the buffer needed ro stay around. If it does not need to outlive the function in which it is created, then what I wrote is fine.

    What I mean is that the C-string's lifetime is tied to the temporary std::string returned by os.str(), not to the std::ostringstream:

    int main()
    {
    	float value = 3.14f;
    	std::ostringstream os;
    	os << value;
    	const char* buffer = os.str().c_str();
    	// using buffer for anything here other than resetting the pointer value would/should crash
    	return 0;
    }

    To actually use buffer, the std::string returned by os.str() needs to be retained as something other than a temporary.

    • Marked as answer by Nancy Shao Thursday, July 08, 2010 2:16 AM
    Friday, July 02, 2010 1:52 AM
  • ildjarn wrote:
    >      Well, yes, of course, but the OP didn't say how long the buffer needed ro
    >      stay around. If it does not need to outlive the function in which it is
    >      created, then what I wrote is fine.
    >
    > What I mean is that the C-string's lifetime is tied to the temporary std::string
    > returned by os.str(), not to the std::ostringstream:
    >
    > int main()
    > {
    >     float value = 3.14f;
    >     std::ostringstream os;
    >     os<<   value;
    >     const char* buffer = os.str().c_str();
    >     // using buffer for anything here other than resetting the pointer value would/should crash
    >     return 0;
    > }
    >
    > To actually use buffer, the std::string returned by os.str() needs to be
    > retained as something other than a temporary.
     
    Ah yes, you are correct. Using the buffer is OK if you pass the result directly
    to some other function
     
    void f (const char* buf)
    {
     
    }
     
    float value = 3.14f;
    std::ostringstream os;
    os << value;
    f(os.str().c_str());
     
    which is something that I do all the time (and might well be what the OP wanted).
     
     

    David Wilkinson | Visual C++ MVP
    • Marked as answer by Nancy Shao Thursday, July 08, 2010 2:16 AM
    Friday, July 02, 2010 2:20 AM

All replies

  • I have a question, could you tell me how to convert from float to char? 


    Take a look at sprintf or std::stringstream.  sprintf is an option that is available in C or C++.  std::stringstream is just for C++.  Here is an example:

    #include <cstdio>
    #include <sstream>

    int main()
    {
        float value = 3.14f;

        char buffer[256];
        sprintf(buffer, "%f", value);

        std::stringstream ss;
        ss << value;
        ss >> buffer;
    }

     

    Some things to consider:

    - When using arrays, you have to make sure the array is big enough to hold the floating point number once it has been turned to text.  A string class can make this easier, like std::string or CString.

    - You will want to consider how you want the floating number to be represented.  For instance, 3.14 could be 3.14000 or 03.14, etc.  If you lookup sprintf and stringstream you will can learn how to get different formats.

    - There might be a variant of sprintf that your platform offers that can be safer.  For instance, something like sprintf_s

    - You could also look into _gcvt and similar functions.

    Thursday, July 01, 2010 7:17 PM
  • #include <cstdio>
    #include <sstream>
     
    int main()
    {
        float value = 3.14f;
     
        char buffer[256];
        sprintf(buffer, "%f", value);
     
        std::stringstream ss;
        ss << value;
        ss >> buffer;
    }
    If you do not need to modify the buffer, a simpler way is
     
    int main()
    {
        float value = 3.14f;
        std::ostringstream os;
        os << value;
        const char* buffer = os.str().c_str();
        return 0;
    }

    David Wilkinson | Visual C++ MVP
    Thursday, July 01, 2010 7:51 PM
  • David, unfortunately that won't work, as buffer points to the data of a temporary string; the string needs to stick around for as long as you need buffer to stick around:

    int main()
    {
    	float value = 3.14f;
    	std::ostringstream os;
    	os << value;
    	std::string str = os.str();
    	const char* buffer = str.c_str();
    	return 0;
    }
    Thursday, July 01, 2010 8:06 PM
  • David, unfortunately that won't work, as buffer points to the data of a temporary string; the string needs to stick around for as long as you need buffer to stick around:
     
    int main()
    {
        float value = 3.14f;
        std::ostringstream os;
        os << value;
        std::string str = os.str();
        const char* buffer = str.c_str();
        return 0;
    }
    Well, yes, of course, but the OP didn't say how long the buffer needed ro stay around. If it does not need to outlive the function in which it is created, then what I wrote is fine.

    David Wilkinson | Visual C++ MVP
    Friday, July 02, 2010 1:20 AM
  • Well, yes, of course, but the OP didn't say how long the buffer needed ro stay around. If it does not need to outlive the function in which it is created, then what I wrote is fine.

    What I mean is that the C-string's lifetime is tied to the temporary std::string returned by os.str(), not to the std::ostringstream:

    int main()
    {
    	float value = 3.14f;
    	std::ostringstream os;
    	os << value;
    	const char* buffer = os.str().c_str();
    	// using buffer for anything here other than resetting the pointer value would/should crash
    	return 0;
    }

    To actually use buffer, the std::string returned by os.str() needs to be retained as something other than a temporary.

    • Marked as answer by Nancy Shao Thursday, July 08, 2010 2:16 AM
    Friday, July 02, 2010 1:52 AM
  • ildjarn wrote:
    >      Well, yes, of course, but the OP didn't say how long the buffer needed ro
    >      stay around. If it does not need to outlive the function in which it is
    >      created, then what I wrote is fine.
    >
    > What I mean is that the C-string's lifetime is tied to the temporary std::string
    > returned by os.str(), not to the std::ostringstream:
    >
    > int main()
    > {
    >     float value = 3.14f;
    >     std::ostringstream os;
    >     os<<   value;
    >     const char* buffer = os.str().c_str();
    >     // using buffer for anything here other than resetting the pointer value would/should crash
    >     return 0;
    > }
    >
    > To actually use buffer, the std::string returned by os.str() needs to be
    > retained as something other than a temporary.
     
    Ah yes, you are correct. Using the buffer is OK if you pass the result directly
    to some other function
     
    void f (const char* buf)
    {
     
    }
     
    float value = 3.14f;
    std::ostringstream os;
    os << value;
    f(os.str().c_str());
     
    which is something that I do all the time (and might well be what the OP wanted).
     
     

    David Wilkinson | Visual C++ MVP
    • Marked as answer by Nancy Shao Thursday, July 08, 2010 2:16 AM
    Friday, July 02, 2010 2:20 AM
  • Agreed. :-] I just wanted to clarify for the edification of the OP, who may not know the difference.
    Friday, July 02, 2010 2:30 AM
  • char str[40];
    float f = 1.5F;
    sprintf(str, "%f", f);
    Friday, July 02, 2010 1:58 PM