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. CPU监控 解题报告

    CPU监控 这种题就需要小黄鸭调试法,不行就重构,动态gdb可能会死人,一堆tag的... 维护历史最值的一个核心是历史最值tag,它的意义是从上一次这个点下放tag之后到当前时刻的这个点的tag达到 ...

  2. django 模板语言

    母版与继承: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  3. cobbler无人值守自动安装

      无人值守安装(自动按定制的需求来批量安装linux系统) kickstart cobbler linux安装系统的方法: 1,光盘 把iso镜像刻录(windows下有类似nero这类软件)到光盘 ...

  4. Vagrant 遇到的问题

    Vagrant was unable to mount VirtualBox shared folders. This is usuallybecause the filesystem "v ...

  5. QML学习笔记(三)-引入Font-awesome

    作者: 狐狸家的鱼 Github: 八至 1.首先得在qml文件夹下建立字体文件,将font-awesome放入进去 2.然后在main.cpp中注册字体 引入中一定要写上 引用字体 引用字体得路径一 ...

  6. HDU/HDOJ 4699 Editor

    对顶栈算法. 此题充分说明了cin的不中以及scanf的优越性. 我TM用cin超时了!!!换成scanf就A了!!! #include <cstdio> #include <cst ...

  7. 【洛谷P3810】陌上花开

    题目大意:给定一个三维空间点的坐标,求对于任意一个点三维均小于等于这个点的点个数. 题解:学会了简单的 cdq 分治. 首先,先将第一维从小到大排序,再用类似归并排序的操作对第二维进行排序,在第二维合 ...

  8. 编译:ffmpeg,精简ffmpeg.exe

    网上下载的各种 ffmpeg.exe ,最少都有11M+ 而我只需要处理 mp4 和 mp3,在网上搜索了一下精简ffmpeg的文章,折腾一天,也没有完全搞定,但多少有些收获,记录一下: 从 www. ...

  9. 关于form与表单操作

    form表单自动提交规则 form表单中只有一个type=text的input,在input中按enter键,会自动提交: form表单中有多个type=text的input,且无type=submi ...

  10. 表格中的checkbox复选框 全选非全选 公共方法 及提交选中结果

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...