NO

Author Topic: Conversion of double to wchar_t*  (Read 2686 times)

CplEthane

  • Guest
Conversion of double to wchar_t*
« on: June 02, 2020, 06:21:11 AM »
What I'm doing is taking  from user input a decimal number (GetWindowTextW) to a char array (wcstombs) to a double (strtod) and then through a function, do a calculation to return a double value (let's just say it's (double a+ double b)). Now, I want to take that double value and output it on my Window so the user can see the result, and I can't figure out how to do this.  Everything I've researched on how to accomplish this is either wrong or doesn't work with Pelles or more likely I'm just doing something wrong. Here is the function I have in order to attempt this:

Code: [Select]
wchar_t *ConvertDoubleToWchar(double value)
{
wchar_t ValOut[10];
char ValueResult[10] = {0};
snprintf(ValueResult, 10, "%.3f", value);
swprintf(ValOut, 10, L"%hs", ValueResult);
return ValOut;
}

But when I try to implement as such: CreateWindowW(L"Static", *ConvertDoubleToWchar(double value) etcetc...,  no value appears on the window.  It's always blank.  Is this a casting issue on my part?  I've tried so many things to try to get this to work that I've lost track of them. My code compiles, but it just won't display the double value that I'm going for.

Offline John Z

  • Member
  • *
  • Posts: 860
Re: Conversion of double to wchar_t*
« Reply #1 on: June 02, 2020, 01:06:50 PM »
Hi,

First thing is to turn on Level 2 or at least Level 1 warnings in the Project Options Compiler tab and compile.

This line swprintf(ValOut, 10, L"%hs", ValueResult); will show an error.
ValueResult is a string but the format specifier is using "h" which is used as a numeric specifier.

Change the format specifier to just L"%s".

Hope this helps,
Regards

CplEthane

  • Guest
Re: Conversion of double to wchar_t*
« Reply #2 on: June 02, 2020, 05:09:08 PM »
John,

Thanks so much for responding.  I did what you suggested, and set the warnings to level 2 and dropped the h specifier, and one of the warnings I received was about how a pointer to local was an invalid return value on that very function, so I fixed that as well. It's still not working, however.  Is there some solid example code for Pelles for what I'm attempting here?  That would probably get me on the right track quicker than anything. Again, thanks.

CplEthane

  • Guest
Re: Conversion of double to wchar_t*
« Reply #3 on: June 02, 2020, 05:50:29 PM »
Nevermind, I got it working. I dispensed with the function and simply handled the conversion locally and that works which is less elegant but at least I have a working foundation to go from. Thanks again!

Offline John Z

  • Member
  • *
  • Posts: 860
Re: Conversion of double to wchar_t*
« Reply #4 on: June 02, 2020, 06:27:15 PM »
Hi,

Glad that helped.

The other issue is you need to consider the scope (lifetime) of your variables.
The variable ValOut is declared within the procedure so it will not be available
once the procedure terminates.  So you can see returning a pointer to a deceased
variable won't return your answer.

Make ValOut a global variable or passing in a pointer to it from the calling procedure
where it is created, is how to have your elegance restored :)

functionality over form in the beginning …..

wchar_t *ConvertDoubleToWchar(double value, wchar_t *p_ValOut)  for example

Regards,
John

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Conversion of double to wchar_t*
« Reply #5 on: June 04, 2020, 07:38:40 AM »
wchar_t *gcvtw(double f, int ndigit, wchar_t *buf)

https://forum.pellesc.de/index.php?topic=7218.msg27430#msg27430
May the source be with you