string与wstring之间的转换
2016-07-27 18:37
369 查看
#include <string>
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;
}
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;
}
相关文章推荐
- Ubuntu 14.04 LTS 实现双机文件同步lsync和rsync
- Java同步队列(非阻塞队列与阻塞队列)——java并发容器
- 渐渐明白习以为常的珍贵
- POJ1830 开关问题【 高斯消元】
- Java数组的几种常用操作方法(排序算法及查找)
- 【RabbitMQ 参考资料】
- Codeforces Round #301 (Div. 2) D. Bad Luck Island(概率DP)
- python 画图--饼图
- Codeforces Round #340 (Div. 2) C. Watering Flowers(暴力枚举)
- 冒泡排序
- 模块度Q——复杂网络社区划分评价标准
- 基于libfiber的高并发服务优化策略
- 手势识别
- Leetcode题集——jump-game
- ios Cookies
- matlab作图如何改变坐标刻度
- 2016.07.16【初中部 NOIP提高组 】模拟赛C
- Libevent源码分析-----多线程、锁、条件变量(一)
- mysql性能优化-慢查询分析、优化索引和配置
- 第三章--不得不学的Fragment