首页 > 编程语言 >C++ wchar_t char wstring string 转换

C++ wchar_t char wstring string 转换

时间:2022-12-01 10:59:59浏览次数:35  
标签:std string int C++ wstring str buff

1.wchart_t转wstring

1 wchar_t tmpRuleStr[10] = {0};
2 wstring m_tmpRuleStr = wstring(tmpRuleStr);

2.wstring转wchar_t

1 wstring str = "123";
2 wchar_t* tmp = wstr.c_str();

3.string转wstring

 1 std::wstring UTF8ToUnicode(const std::string& utf) 
 2 {
 3     wchar_t *buff = new wchar_t[utf.length()+1];
 4     int len = ::MultiByteToWideChar(CP_UTF8, 0, utf.c_str(), (int)utf.length(), buff, (int)utf.length()+1);
 5     std::wstring str(buff, len);
 6     delete [] buff;
 7     return str;
 8 }
 9 std::wstring AnsiToUnicode(const std::string& str) 
10 {
11     wchar_t *buff = new wchar_t[str.length()+1];
12     int len = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), buff, str.length()+1);
13     std::wstring text(buff, len);
14     delete [] buff;
15     return text;
16 }

4.wstring转string

 1     std::string UnicodeToUTF8(const std::wstring& str) 
 2     {
 3         char* buff; 
 4         int buffersize = ::WideCharToMultiByte(CP_UTF8, 0, str.c_str(), (int)str.length(), 0, 0, 0, 0);
 5         buff = new char[buffersize+1];
 6         int len = ::WideCharToMultiByte(CP_UTF8, 0, str.c_str(), (int)str.length(), buff, buffersize+1, 0, 0);
 7         std::string utf(buff, len);
 8         delete []buff; 
 9         return utf;
10     }
11 
12     std::string UnicodeToAnsi(const std::wstring& str) 
13     {
14         char *buff; 
15         int buffersize = ::WideCharToMultiByte(CP_ACP, 0, str.c_str(), str.length(), 0, 0, NULL, NULL); 
16         buff = new char[buffersize+1];
17         int len = ::WideCharToMultiByte(CP_ACP, 0, str.c_str(), str.length(), buff, buffersize + 1, NULL, NULL); 
18         std::string text(buff, len);
19         delete [] buff;
20         return text;
21     }

 

标签:std,string,int,C++,wstring,str,buff
From: https://www.cnblogs.com/ybqjymy/p/16940692.html

相关文章