简体   繁体   中英

change integer number to wstring in c++

I have the following code, which I am trying ot change an integer number to wstring then I can pass it to a text file. but the problm is that when it reached to the _itow_s(vec, img_stamp, 10); line it change the number to a negative number. any body know what is the problem?

wchar_t img_stamp[64];
   while(c<_timeStamp.size()){      
        vec=_timeStamp.at(c);
        _itow_s(vec, img_stamp, 10);    
        _data +=std::wstring(img_stamp);
        _data +=std::wstring(L"\r\n");
        c++;
    }


    HANDLE hFile; 
    DWORD wmWritten;    
    hFile = CreateFile( "D:\\test\\testing.txt" ,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL); 
    if(hFile!=NULL){

        DWORD len=sizeof(_data);
        WriteFile(hFile, _data.c_str(), _data.size() * 2, &wmWritten, NULL);
    }   
    CloseHandle(hFile); 

Why don't you use a wstringstream ?

wstringstream ws;
for (...) { // some loop
  ws << myint << ", ";
}
//now, you can use ws.str() to extract a wstring from the stream

C++11 added a function called ' std::to_string ()' which converts a number, float, or long long to a std::string. There is also a related set of functions for wstrings: std::to_wstring ().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM