#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;
}

string与wstring之间的转换的更多相关文章

  1. wchar_t char string wstring 之间的转换

    wchar_t char string wstring 之间的转换 转:http://blog.csdn.net/lbd2008/article/details/8333583 在处理中文时有时需要进 ...

  2. 深入理解c++中char*与wchar_t*与string以及wstring之间的相互转换 [转]

    本篇文章是对c++中的char*与wchar_t*与string以及wstring之间的相互转换进行了详细的分析介绍,需要的朋友参考下. #ifndef USE_H_ #define USE_H_ # ...

  3. 深入理解c++中char*与wchar_t*与string以及wstring之间的相互转换

    本篇文章是对c++中的char*与wchar_t*与string以及wstring之间的相互转换进行了详细的分析介绍,需要的朋友参考下-复制代码 代码如下:    #ifndef USE_H_     ...

  4. MFC中char*,string和CString之间的转换

    MFC中char*,string和CString之间的转换 一.    将CString类转换成char*(LPSTR)类型 方法一,使用强制转换.例如:  CString theString( &q ...

  5. String与StringBuffer之间的转换

    来源:http://www.oschina.net/code/snippet_2261089_47352 package demo; /* String与StringBuffer之间的转换 * Str ...

  6. 【Java】【9】String Date Calendar之间的转换

    前言: 1, Calendar 转化 String 2, Calendar 转化 Date 3,Date 转化 String 4,Date 转化 Calendar 5,String 转化 Calend ...

  7. [java]转:String Date Calendar之间的转换

    String Date Calendar之间的转换 String Date Calendar  1.Calendar 转化 String Calendar calendat = Calendar.ge ...

  8. string和数值之间的转换

    string和数值之间的转换 to_string(val) 一组重载函数,返回数值val的string表示val可以是任何算数类型. stoi(s,p,b),stol(s,p,b),stoul(s,p ...

  9. list,string,tuple,dictionary之间的转换

    list,string,tuple,dictionary之间的转换 类型 String List tuple dictionary String - list(str), str.split() tu ...

随机推荐

  1. 获取使用ClickOnce部署的应用程序的版本号

    引子 在编写使用ClickOnce部署的应用程序时,需要在程序的标题栏.软件变更记录.软件关于等页面读取显示当前的版本号. 之前很傻瓜的做法就是在Resource中维护一个string值,在使用到的地 ...

  2. URLDecoder解析url编码

    try { strJson = URLDecoder.decode(strJson, "utf-8"); } catch (UnsupportedEncodingException ...

  3. MyBatis参数传入集合之foreach用法

    传入集合list // 账户类型包括门店和分公司 List<Object> scopeList = new ArrayList<Object>(); scopeList.add ...

  4. 解决:error: Cannot fetch repo (TypeError: expected string or buffer)

    同步源码,问题重现: Fetching project platform/external/libopus Fetching project repo error: Cannot fetch repo ...

  5. 解决:MIUI 8应用商店下载不了软件 APP

    MIUI 8应用商店下载不了软件,是什么原因呢? 原因是:刷了国际版的MIUI,然后又刷回国内的MIUI,刷机时数据没有清理干净. 解决办法:使用RE管理器或者其他第三方可以编辑系统文件的文件管理器, ...

  6. css-margin与百分数的关系

    可以对元素的margin设置百分数,百分数是相对于父元素的width计算,不管是margin-top/margin-bottom还是margin-left/margin-right.(padding同 ...

  7. NodeJS:Error: Cannot find module 'jshint/src/cli'

    以前命令:npm install gulp-jshint --save-dev 实质上是安装jshint失败,缺少该模块. 更换命令 :npm install --save-dev jshint gu ...

  8. How to create vlan on Linux (with Cisco Catalyst Switch)

    In this article I want to share to you on how to create and configure vlan on Linux through Cisco Ca ...

  9. MonoGame教程

    http://www.gamefromscratch.com/page/MonoGame-Tutorial-Series.aspx http://rbwhitaker.wikidot.com/mono ...

  10. 增加UBUNTU字符集 解决中文乱码问题

    对GBK,GB2312,GB18030字符集的支持是UBUNTU中文乱码的罪魁祸首,其实我们可以在保持UTF-8为默认编码的条件下添加对这几个编码的支持,以解决中文乱码问题. 我想这个问题肯定有其他人 ...