Convert CString to float!!

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!
killaz4lifeAsked:
Who is Participating?
 
AndyAinscowFreelance programmer / ConsultantCommented:
     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
0
 
jkrCommented:
Try

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

0
 
killaz4lifeAuthor Commented:
I did, but I still keep getting that stupid value, not getting the value i am inputting?
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
jkrCommented:
How exactly are you trying to do that conversion? (in terms of code ;o)
0
 
killaz4lifeAuthor Commented:
u got it up there, wut i wrote!
0
 
mahesh1402Commented:
just a quick'n dirty solution:

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


-MAHESH
0
 
Jase-CoderCommented:
Hello,

try:       

CString T = "123.456";
float Z = atof(T.GetBuffer(T.GetLength()));
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
ps
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)
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
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.
0
 
killaz4lifeAuthor Commented:
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?
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
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?
0
 
waysideCommented:
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.



0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.