Wednesday, 15 August 2012

c++ - cast float to wchar_t win32 -



c++ - cast float to wchar_t win32 -

it freaks me out cannot find anyway cast float wchar_t or maybe looking in wrong places!

float cnumbers[9] = {1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0}; float x = 3.0; float temp = 0.0; wchar_t data[] = {0}; for(int i=0; < sizeof(cnumbers); i++){ temp = x / cnumbers[i]; bool isint = temp == static_cast<int>(temp); if(isint){ info = temp; //this big fail addtolist(hwnd,data); } } void addtolist(hwnd hwnd,const wchar_t * info ){ sendmessage(getdlgitem(hwnd,idc_listbox),lb_addstring,0,(lparam)data); }

the problem want convert float value wchar_t send listbox

in c++, casting pods reinterpret binary info casted-to type , not kind of type conversion want accomplish.

you have couple of options here:

you can utilize boost::lexical_cast cast-like conversion you can utilize wsprintf or std::wstringstream handle conversion float wide string if using mfc/atl, can utilize cstring::format convert float string

the sec alternative 1 doesn't utilize third-party library if restricted in can utilize library wise, you're stuck either wsprintf or std::wstringstream. recommendation in case utilize std::wstringstream both type safety reasons , protections buffer overflows.

c++ windows winapi visual-studio-2012

No comments:

Post a Comment