一杯清酒邀明月
天下本无事,庸人扰之而烦耳。
posts - 3121,comments - 209,views - 578万

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     }
复制代码

 

posted on   一杯清酒邀明月  阅读(733)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
历史上的今天:
2020-12-01 Gamma、Linear、sRGB 和Unity Color Space,你真懂了吗?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示