从“std::string”转换为“const wchar_t” 从 “const char *”转换为“const wchar_t *”
stringstr2="add";constchar*psc;psc=str2.c_str();MessageBoxA(this->m_hWnd,psc,NULL,MB_...
string str2="add";
const char *psc;
psc=str2.c_str();
MessageBoxA(this->m_hWnd,psc,NULL,MB_OK);
const wchar_t *psc2;
psc2=psc; //无法从“const char *”转换为“const wchar_t *”
*psc2=str2; //无法从“std::string”转换为“const wchar_t”
*psc2=_T(str2); //“Lstr2”: 未声明的标识符
MessageBox(psc2); 展开
const char *psc;
psc=str2.c_str();
MessageBoxA(this->m_hWnd,psc,NULL,MB_OK);
const wchar_t *psc2;
psc2=psc; //无法从“const char *”转换为“const wchar_t *”
*psc2=str2; //无法从“std::string”转换为“const wchar_t”
*psc2=_T(str2); //“Lstr2”: 未声明的标识符
MessageBox(psc2); 展开
1个回答
展开全部
std::string ws2s(const std::wstring& ws)
{
std::string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C";
setlocale(LC_ALL, "chs");
const wchar_t* _Source = ws.c_str();
size_t _Dsize = 2 * ws.size() + 1;
char *_Dest = new char[_Dsize];
memset(_Dest,0,_Dsize);
wcstombs(_Dest,_Source,_Dsize);
std::string result = _Dest;
delete []_Dest;
setlocale(LC_ALL, curLocale.c_str());
return result;
}
std::wstring s2ws(const std::string& s)
{
setlocale(LC_ALL, "chs");
const char* _Source = s.c_str();
size_t _Dsize = s.size() + 1;
wchar_t *_Dest = new wchar_t[_Dsize];
wmemset(_Dest, 0, _Dsize);
mbstowcs(_Dest,_Source,_Dsize);
std::wstring result = _Dest;
delete []_Dest;
setlocale(LC_ALL, "C");
return result;
}
上面那个是wstring转换为string的,下面的反之,你试试
{
std::string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C";
setlocale(LC_ALL, "chs");
const wchar_t* _Source = ws.c_str();
size_t _Dsize = 2 * ws.size() + 1;
char *_Dest = new char[_Dsize];
memset(_Dest,0,_Dsize);
wcstombs(_Dest,_Source,_Dsize);
std::string result = _Dest;
delete []_Dest;
setlocale(LC_ALL, curLocale.c_str());
return result;
}
std::wstring s2ws(const std::string& s)
{
setlocale(LC_ALL, "chs");
const char* _Source = s.c_str();
size_t _Dsize = s.size() + 1;
wchar_t *_Dest = new wchar_t[_Dsize];
wmemset(_Dest, 0, _Dsize);
mbstowcs(_Dest,_Source,_Dsize);
std::wstring result = _Dest;
delete []_Dest;
setlocale(LC_ALL, "C");
return result;
}
上面那个是wstring转换为string的,下面的反之,你试试
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询