Re: CComboBox-Droplist: float values?
"BobF" <rNfOrSePeAzMe@charter.net> ha scritto nel messaggio
news:Or7iQquIIHA.3848@TK2MSFTNGP05.phx.gbl...
"David Wilkinson" <no-reply@effisols.com> wrote in message [...]
Why not just use atof() on the strings and compare the results?
David, I've been down that road. For some reason, GetWindowTextW returns
only the '.' of strings formatted similarly to "0.25". Obviously not
enough for atof to work with :-((
Bob: I have not read your code, but I think that you may be doing a wrong
cast from wide string (wchar_t *) returned by GetWindowTextW to ANSI (char
*) and passing it to atof.
Example:
If you insert ".25" in the edit box, and you read it with GetWindowTextW, I
think it will return the following bytes in the (Unicode UTF-16) string:
Character: '.'; hex bytes: 2E 00
Character: '2'; hex bytes: 32 00
Character: '5'; hex bytes: 35 00
End of string: hex bytes: 00 00
corresponding to byte sequence:
2E 00 32 00 35 00 00 00
i.e. the UTF-16 encoding of ".25".
Then, if you cast to char *, atof gets the ANSI string (same bytes as above,
but different interpretation):
2E 00 .... <ignored>
in fact, atof thinks that 00 is end-of-string ('\0'), so it just reads the
2E, which is the '.' character in ANSI.
So, it is like passing just "." to atof().
This is why is important to use functions like _wtof or _tstof.
....or convert input string from wide to ANSI *before* passing it to atof
(using e.g. CW2A or CStringA conversion constructor).
This code seems fine on VS2003, also in Unicode:
<code>
// Button Click handler
// Read user floating point string
CString s;
m_edit.GetWindowText(s);
s.Trim();
if ( s.IsEmpty() )
{
AfxMessageBox( _T("Please enter a floating-point number...") );
return;
}
// Convert to floating point
double x = _tstof(s);
// Test result
CString fmt;
fmt.Format( _T("Number: %g"), x );
AfxMessageBox( fmt );
</code>
Giovanni