PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Unicode to float?

Is there an equivalent function to atof but that can handle Unicode or WCHAR. I searched the forum, but couldn't find anything.

Thanks.
I'm using PocketGCC.

In stdlib.h I have
#define _wtoi _wtol
#define _wtoi64 _wtoll

but, _wtof seems to be missing and there's no wchar.h file. Now what? :?
Since PocketGCC is a port of GCC try:

wcstof

http://www.delorie.com/gnu/docs/glibc/libc_424.html

Otherwise, you also might want to post your question here:

http://groups.yahoo.com/group/pocketgcc/


Hmmm... or you could always convert the unicode to ansi and then use atof. 8O


Good luck!
Seems I couldn't find wcstof either.

What I'm trying to do is get a float that was input from a dialog box.

In the meantime, I tried using wcstombs, but I'm getting an "invalid conversion from 'UINT' to 'const wchar_t*' Any idea what I'm doing wrong?

Here's a code snippet:

Code:

float size_x;
char *pmbbuf = (char *)malloc(100);

int len = GetWindowTextLength(GetDlgItem(hwnd, IDC_SIZE_X));
if(len > 0)
{
int i;
WCHAR* buf;
buf = (WCHAR*)GlobalAlloc(GPTR, len + 1);
wcstombs(pmbbuf,GetDlgItemText(hwnd, IDC_SIZE_X, buf, len + 1), 100);
size_x=atof(pmbbuf);

GlobalFree(HANDLE)buf);

}
GetDlgItemText returns a UINT to indicate success or failure. You can't use it in place of a string pointer.

Dan East
wcstombs(pmbbuf,GetDlgItemText(hwnd, IDC_SIZE_X, buf, len + 1), 100);


The return type of GetDlgItemText is UINT. Seems you want to convert an UNINT to WCHAR*.
I would rather use buf for the conversion.

Tala
You're not allocating enough memory for the wide character string. Each character is two bytes:

GlobalAlloc(GPTR, (len + 1) * sizeof(WCHAR));

Dan East
Thanks, guys! :D

Your suggestions worked.

For a while, I couldn't figure out why my float was still coming out to 0.000. It was because I had this snippet of code in my WndProc function and then realized it needed to be in my DlgProc function.

Here's the revised code snippet:

Code:

float size_x;
char *pmbbuf = (char *)malloc(100);

int len = GetWindowTextLength(GetDlgItem(hwnd, IDC_SIZE_X));
if(len > 0)
{
char *pmbbuf = (char *)malloc(100);
WCHAR* buf;

buf = (WCHAR*)GlobalAlloc(GPTR, len + 1 * sizeof(WCHAR));
GetDlgItemText(hwnd, IDC_SIZE_X, buf, len + 1);
wcstombs(pmbbuf, buf, 100);

size_x=atof(pmbbuf);

GlobalFree(HANDLE)buf);

}
This is still wrong:
len + 1 * sizeof(WCHAR)

It should be:
(len + 1) * sizeof(WCHAR)

So your buffer will be too small if your string is longer than 1 character.

You have two different pmbbuf variables, each with a different scope, and neither appears to be freed. If you are going to hardcode it to dynamically allocate 100 bytes, then you might as well just use an array instead:

char pmbbuf[100];

Personally, I would reduce all of that down to this:
Code:

float size_x;
char sBuf[100];
WCHAR swBuf[100];

if (GetDlgItemText(hwnd, IDC_SIZE_X, swBuf, sizeof(swBuf)/sizeof(WCHAR))) {
wcstombs(sBuf, swBuf, sizeof(sBuf));
size_x=atof(sBuf);
} else
size_x=0; //Default value if edit box is empty


If you could use _wtof:

Code:

float size_x;
WCHAR swBuf[100];

if (GetDlgItemText(hwnd, IDC_SIZE_X, swBuf, sizeof(swBuf)/sizeof(WCHAR)))
size_x=_wtof(swBuf);
else
size_x=0; //Default value if edit box is empty



Dan East
joshbu [MSFT] · 21 May 2004, 01:39 UTC
Just out of mild curiosity, why do you need to take user input and make a float out of it? You should be aware that there is a problem with this approach. It is always possible to enter a number into the dialog that is not exactly representable with floating point. (Even 64bit floating point.) So, if you are going to display this value back to the user, then (s)he might see a different number than what they entered.

Anyhoo, they swscanf function should do what you're looking for directly, so you don't need the extra conversion from wide->narrow chars.
Trivial example: 0.1
I'm using the dialog box to set size, rotation and location values for 3d primitives.

Thanks again for the help.