baxteras

hi everyone, i'm new to win32 programming , especially for smart devices.

i'm developping an application and i need to show some float variables in edit boxes. so first i need to convert them to strings.

here is the code i use:

      LPTSTR buff = new TCHAR[256+1];
      sprintf((char*)buff,"%g", x_coord);
      SetDlgItemText(hDlg, IDC_XCOORD, buff);
      delete[] buff;

 

someone told me to use _stprintfl_s instead of sprintf but it seems that is not supported by evc++ 4 i tried to include several files that i read in msdn but with no luck..

so to be quick , with this code i get as output those strange squares instead of my float...

any idea

thanx a lot



Re: Smart Devices Native C++ Development EVC++ 4.0 : float to string conversion unicode problem?

Ilya Tumanov

Here's a hint: never use char, never use TCHAR - always use WCHAR. Also always use Unicode versions of everything:

swprintf(buff, L"%g", x_coord);

This way you will be globalization ready and your strings would be consistent. You also won¡¯t have to ¡°shut up¡± compiler yelling at you about incorrect types by applying wrong cast like you did here:

sprintf((char*)buff,"%g", x_coord);






Re: Smart Devices Native C++ Development EVC++ 4.0 : float to string conversion unicode problem?

baxteras

you were right, i did this and worked

LPTSTR buff = new TCHAR[256+1];
_stprintf(buff, L"%g", x_coord);
SetDlgItemText(hDlg, IDC_XCOORD, buff);
delete[] buff;

solved this but now i came up with another problem:

len = GetWindowTextLength(GetDlgItem(hDlg, IDC_XCOORD));
if (len>0) {
LPTSTR buf = new TCHAR[len+1];
GetDlgItemText(hDlg, IDC_XCOORD, buf, len + 1);
_stscanf(buf, L"%f", &tmp);
x_coord = tmp;
SetDlgItemText(m_hWnd, IDS_XBOX, buf);
delete[] buf;
}

i want to convert string to float, i tried to find an appropriate atof function but is seems there isnt any, so i thought using _stscanf. the problem is that it seems tmp doesnt get any value.. it has its original value from initialization..





Re: Smart Devices Native C++ Development EVC++ 4.0 : float to string conversion unicode problem?

Ilya Tumanov

_stscanf would do that if it can not parse the string according to format, e.g. because string is incorrect. You should always check whatever _stscanf returns to make sure conversion was a success. Also print out string you're passing to _stscanf to make sure it's good, try known good hard coded string to see if the rest of your code works.