std::wstring StringToWString(const std::string& str)
{
int num = MultiByteToWideChar(CP_UTF8, , str.c_str(), -, NULL, );
wchar_t *wide = new wchar_t[num];
MultiByteToWideChar(CP_UTF8, , str.c_str(), -, wide, num);
std::wstring w_str(wide);
delete[] wide;
return w_str;
} std::string WStringToString(const std::wstring &wstr)
{
std::string str;
int nLen = (int)wstr.length();
str.resize(nLen, ' ');
int nResult = WideCharToMultiByte(CP_ACP, , (LPCWSTR)wstr.c_str(), nLen, (LPSTR)str.c_str(), nLen, NULL, NULL);
if (nResult == )
{
return "";
}
return str;
}

如果程序有时候用utf-8中文的话,一般从网络传回的数据是utf-8,StringToWString后是乱码,那么需要使用下面方案

std::wstring StringToWString(const std::string& str)
{
setlocale(LC_ALL, "chs");
const char* point_to_source = str.c_str();
size_t new_size = str.size() + ;
wchar_t *point_to_destination = new wchar_t[new_size];
wmemset(point_to_destination, , new_size);
mbstowcs(point_to_destination, point_to_source, new_size);
std::wstring result = point_to_destination;
delete[]point_to_destination;
setlocale(LC_ALL, "C");
return result;
}

C++ WString与String互相转换的更多相关文章

  1. C++中Cstring、wstring 和string互相转换总结

    通过前一篇文章<C++中string,wstring,CString的基本概念和用法>,对Cstring.wstring 和string有了一个了解.string是C++提供的标准字符串操 ...

  2. wchar_t与char、wstring与string的相互转换

    个人倾向于使用优秀的开源库做这个. 最近使用boost进行转换,代码极其简单: boost::filesystem::path src(wchar_t); char = src.string().c_ ...

  3. VC++中 wstring和string的互相转换实现

    在VC++开发中,经常会用到string和wstring,这就需要二者之间的转换,项目中封装了wstring和string相互转换的2个函数,实现如下: //将wstring转换成string std ...

  4. C# Byte[] 转String 无损转换

    C# Byte[] 转String 无损转换 转载请注明出处 http://www.cnblogs.com/Huerye/ /// <summary> /// string 转成byte[ ...

  5. CString 转 char*; wstring 转 string

    1. CString  转 char* ); CString name; name.Format(_T("bookUC%d.txt"),m_ID); std::wstring _n ...

  6. C# 之 将string数组转换到int数组并获取最大最小值

    1.string 数组转换到 int 数组 " }; int[] output = Array.ConvertAll<string, int>(input, delegate(s ...

  7. 转:char*, char[] ,CString, string的转换

    转:char*, char[] ,CString, string的转换 (一) 概述 string和CString均是字符串模板类,string为标准模板类(STL)定义的字符串类,已经纳入C++标准 ...

  8. HTML5 Blob与ArrayBuffer、TypeArray和字符串String之间转换

    1.将String字符串转换成Blob对象 //将字符串 转换成 Blob 对象 var blob = new Blob(["Hello World!"], { type: 'te ...

  9. 【python】bytearray和string之间转换,用在需要处理二进制文件和数据流上

    最近在用python搞串口工具,串口的数据流基本读写都要靠bytearray,而我们从pyqt的串口得到的数据都是string格式,那么我们就必须考虑到如何对这两种数据进行转换了,才能正确的对数据收发 ...

随机推荐

  1. django后台密码错误

    如果你忘记了设置Django的Admin密码,那么你可以使用createsuperuser来甚至密码,但是如果你忘记了Admin的密码的话,那么就要用Django shell: 1 python ma ...

  2. tyvj/joyoi 2018 小猫爬山

    2018,这个题号吼哇! 搜索第一题,巨水. WA了一次,因为忘了还原... #include <cstdio> ; int n, W, ans, weigh[N], cost[N]; i ...

  3. A1117. Eddington Number

    British astronomer Eddington liked to ride a bike. It is said that in order to show off his skill, h ...

  4. C/C++ 动态存储分配 malloc calloc realloc函数的用法与区别

    C++内存分配 https://blog.csdn.net/zhangxiao93/article/details/43966425

  5. java操作redis集群配置[可配置密码]和工具类(比较好用)

    转: java操作redis集群配置[可配置密码]和工具类 java操作redis集群配置[可配置密码]和工具类     <dependency>   <groupId>red ...

  6. java 分隔函数split("",-1)的用途

    转: java 分隔函数split("",-1)的用途 2017年12月14日 11:37:58 jaryle 阅读数:8517   1.如果字符串最后一位有值,则没有区别, 2. ...

  7. POJ 3349 Snowflake Snow Snowflakes (Hash)

    Snowflake Snow Snowflakes Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 48646   Accep ...

  8. 使用docker-compose部署nginx

      1.新建docker-compose.yml文件,文件的基本模板如下:(由于yml格式比较严格,注意空格缩进) version: '2.0' services: nginx: restart: a ...

  9. qml: 打包 和 发布

    Qt 提供了打包工具windeployqt, 利用该工具可以很方便的解决qt的依赖问题(注:通过实际验证,发现该工具只能解决大部分的依赖问题,不知道是不是本人 没有正确的使用的问题). qt源码编译r ...

  10. springboot配置jsp

    spring.mvc.view.prefix= /WEB-INF/jsp/ spring.mvc.view.suffix= .jsp pom.xml <!--jsp支持--> <!- ...