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

1、std::wstring 转 std::string

复制代码
 1 string WstringToString(const std::wstring wstr)
 2 {
 3 #if 1
 4     std::string result;
 5     int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), NULL, 0, NULL, NULL);
 6     if( len <= 0 )
 7         return result;
 8  
 9     char* buffer = new char[len + 1];
10     if(buffer == NULL )
11         return result;
12  
13     WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), buffer, len, NULL, NULL);
14     buffer[len] = '\0';
15     result.append(buffer);
16     delete[] buffer;
17  
18     return result;
19 #else
20     //unsigned len = (unsigned)str.size() * 4;
21     setlocale(LC_CTYPE, "");
22     //char *p = new char[len];
23     //wcstombs(p,str.c_str(),len);
24     //std::string str1(p);
25     //delete[] p;
26     //return str1;
27 #endif
28 }
复制代码

2、std::string 转 std::wstring

复制代码
 1 wstring StringToWString(const string str) 
 2 {
 3     //int num = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);
 4     //wchar_t *wide = new wchar_t[num];
 5     //MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, wide, num);
 6     //std::wstring w_str(wide);
 7     //delete[] wide;
 8     //return w_str;
 9  
10     wstring result;  
11     //获取缓冲区大小,并申请空间,缓冲区大小按字符计算  
12     int len = MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), NULL, 0);  
13     TCHAR* buffer = new TCHAR[len + 1];  
14     //多字节编码转换成宽字节编码  
15     MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), buffer, len);  
16     buffer[len] = '\0';             //添加字符串结尾  
17     //删除缓冲区并返回值  
18     result.append(buffer);  
19     delete[] buffer;  
20     return result;
21  
22 }
复制代码

 

posted on   一杯清酒邀明月  阅读(658)  评论(0编辑  收藏  举报
编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
< 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

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