Convert CString to float!!

Posted on 2006-06-01
Last Modified: 2013-11-20
m_gVal is a Cstring
float num = atof(m_gVal);

This code does not work since it loads not the value instead some other value, I am geussing where the data is being loaded from, thats the value we got.

after running the program the value of m_gVal = {0x00325ff0 "0"} so I want the value in between the ""

thanks and please do not refer to websites, please fix what my problem is!
Question by:killaz4life
    LVL 86

    Expert Comment


    float num = atof(m_gVal.GetBuffer(0));


    Author Comment

    I did, but I still keep getting that stupid value, not getting the value i am inputting?
    LVL 86

    Expert Comment

    How exactly are you trying to do that conversion? (in terms of code ;o)

    Author Comment

    u got it up there, wut i wrote!
    LVL 22

    Expert Comment

    just a quick'n dirty solution:

    CString m_gVal = _T("123.456");
    float num;
    _stscanf(m_gVal, _T("%f"), &num);

    LVL 11

    Expert Comment



    CString T = "123.456";
    float Z = atof(T.GetBuffer(T.GetLength()));
    LVL 43

    Accepted Solution

         CString s(_T("{0x00325ff0 \"123.45\"}"));
          int n = s.Find(_T('"'));
          CString s1(s.Right(s.GetLength() - n - 1));
          float num = _tstof(s1);

    num now has the value 123.45
    LVL 43

    Assisted Solution

    CString s(_T("{0x00325ff0 \"123.45\"}"));  // s = {0x00325ff0 "123.45"}

    also note the _tstof instead of atof (copes with ANSII and UNICODE conversion, atof won't cope with UNICODE)
    LVL 43

    Expert Comment

    Extract from help
    The input string is a sequence of characters that can be interpreted as a numerical value of the specified type. The function stops reading the input string at the first character that it cannot recognize as part of a number.

    Your string had a { as the first character - so the conversion would stop there and return 0 as there was no number specified.
    What I did was to find the first instance of " in your string as you said the number you wanted converted started after that, then prepare a second string (for clarity - not necessarily good programming) and the conversion to the float was performed on the second string.

    Author Comment

    I tried what you said and it didnt make any difference, i get something called bad pointer!
    But the example you got there any is the right 1!!! You understood the question right your on the right path. I honestly do not know why this is happening. Plus I can assure you, i didnt make those values, the first value came from no where?
    LVL 43

    Expert Comment

    I'm puzzled.
    In your question you say the string is {0x00325ff0 "0"}, now you say you are getting a bad pointer.  How did you find out the value of the string when you asked the question?
    LVL 14

    Assisted Solution

    Are you seeing

    {0x00325ff0 "0"}

    when you hover the mouse over it in the debugger or you look at it in the QuickWatch window?

    Or does the string actually contain a left brace, then a zero, then an x, then a zero, etc.?

    What you show as the contents  ( m_gVal = {0x00325ff0 "0"}  ) exactly matches how the debugger displays a CString, so it is not clear to me exactly what the string displays.

    This program:

    #include "stdafx.h"
    int main(int argc, char*argv[])
      CString foo("0");
      float f = atof(foo);
      printf("string foo is %f\n", f);

       return 0;

    prints out

    string foo is 0.000000

    No casting or GetBuffer() is needed for using CString in atof(); since CString has a (LPCSTR) operator defined, and atof() takes a LPCSTR, the compiler will do the right thing.


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Innovation. Quantification. And Ochestration 3 63
    Doc'in system (example?) BA 7 64
    no14 challenge 14 47
    NotAlone Challenge 20 66
    Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
    Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now