简体   繁体   English

在C ++中将整数更改为wstring

[英]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. 我有以下代码,我试图将整数更改为wstring,然后将其传递给文本文件。 but the problm is that when it reached to the _itow_s(vec, img_stamp, 10); 但问题是当它到达_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

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. C ++ 11添加了一个名为“ std :: to_string ()”的函数,该函数将数字,浮点数或long long转换为std :: string。 There is also a related set of functions for wstrings: std::to_wstring (). wstrings还有一组相关的函数: std :: to_wstring ()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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