Answered by:
Why _wtof Function Does Not Work?

Question
-
I am creating a basic application that has two text fields, and I am using the function SendMessage with the corresponding arguments to store the text in two buffers. Everything works fine. The SendMessage returns the string. However, I want to convert the wide-character strings into doubles, so I use _wtof function. But the function returns a bad value. I do not wnt to use atof because the strings are Unicode.
Also, how can I read the zero from the string and store those into the double variable?
Here some code:
TCHAR szBuffer1[100], szBuffer2[100];
SendMessage (hwndControl, EM_GETLINE, 3, (LPARAM) szBuffer1);
_wtof (szBuffer1);
speed =
value = wsprintf (szBuffer2, TEXT ("%d"), speed);
MessageBox (NULL, szBuffer2, TEXT ("Message"), MB_ICONERROR) ;
Thanks in advance!Thursday, June 12, 2008 11:54 PM
Answers
-
I think _wtof is working fine, and you have the wrong type specifier in your wsprintf call. %d indicates an integer, you should be using %f or some other double type specifier.
- Proposed as answer by scorpion007 Friday, June 13, 2008 3:58 AM
- Marked as answer by nobugz Friday, June 13, 2008 5:53 PM
Friday, June 13, 2008 12:34 AM
All replies
-
I think _wtof is working fine, and you have the wrong type specifier in your wsprintf call. %d indicates an integer, you should be using %f or some other double type specifier.
- Proposed as answer by scorpion007 Friday, June 13, 2008 3:58 AM
- Marked as answer by nobugz Friday, June 13, 2008 5:53 PM
Friday, June 13, 2008 12:34 AM -
In addition to ildjarn's reply, you are inconsistent in your character usage.
_wtof and wsprintf is wide character functions, but you are passing them TCHARs.
In MBCS, your code won't compiler. Should be...
speed = _ttof(szBuffer1);
_stprintf(szBuffer2, _T("%f"), speed);- Proposed as answer by scorpion007 Friday, June 13, 2008 3:58 AM
Friday, June 13, 2008 1:40 AM -
In addition to the above, I would recommend you use StringCchPrintf instead of _stprintf because the latter is an unsafe function. You cannot specify a buffer length, so it can easily be overrun.Friday, June 13, 2008 4:00 AM
-
Thanks!
You were right. I was using a bad type identifier in wprintf. Always, I tried to read the double. It returned a zero.Friday, June 13, 2008 7:39 PM -
You aren't initializing szBuffer1 properly. From the EM_GETLINE documentation: "Before sending the message, set the first word of this buffer to the size, in TCHARs, of the buffer. For ANSI text, this is the number of bytes; for Unicode text, this is the number of characters. The size in the first word is overwritten by the copied line."
There's probably a better way to do it, but you can try this:TCHAR szBuffer1[100] = { }; *reinterpret_cast<WORD*>(&szBuffer1[0]) = 100; TCHAR szBuffer2[100] = { }; SendMessage(hwndControl, EM_GETLINE, 3, (LPARAM)szBuffer1); speed = _ttof(szBuffer1); value = _stprintf(szBuffer2, _T("%f"), speed); MessageBox (0, szBuffer2, _T("Message"), MB_ICONERROR);
- Edited by ildjarn Friday, June 13, 2008 8:21 PM Incorporated Leo's suggestion
Friday, June 13, 2008 8:16 PM