开发者

MFC: Convert CStringArray to float, converting just part of the value

I want to convert val.ElementAt(i) to float value开发者_如何学编程 :

 float *d = new float[NMAX];
 char *buffer = new char[128]; 
 CStringArray val;
 //adding some values to val

 buffer = (LPSTR)(LPCSTR)val.ElementAt(i).GetBuffer();
 d[i] = atof(buffer);

as the result in d[i] I have just part of the value(if it was 55 in d is - 5, 666 - 6 ...), help me please!


You shouldn't be assigning buffer; it's bad code (doesn't do what you want). You could use strncpy, but instead, why not just use the CString directly:

d[i] = atof(val.ElementAt(i));

Assuming you're compiling for MBCS, this should work.

BTW, you could also use the operator[] overload, to make the code slightly cleaner, eg:

d[i] = atof(val[i]);

Edit: If you're using UNICODE, you need the dynamic MBCS/UNICODE macro version of atof, which is _ttof. See http://msdn.microsoft.com/en-us/library/hc25t012%28v=VS.90%29.aspx for the full reference. So the code becomes:

d[i] = _ttof(var[i]);

Oh, forgot to include (duh): all these functions return a double, which is not necessarily the same thing as float. So change your result array to doubles, or explicitly cast the result of the conversion.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜