C++ WString与String互相转换
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互相转换的更多相关文章
- C++中Cstring、wstring 和string互相转换总结
通过前一篇文章<C++中string,wstring,CString的基本概念和用法>,对Cstring.wstring 和string有了一个了解.string是C++提供的标准字符串操 ...
- wchar_t与char、wstring与string的相互转换
个人倾向于使用优秀的开源库做这个. 最近使用boost进行转换,代码极其简单: boost::filesystem::path src(wchar_t); char = src.string().c_ ...
- VC++中 wstring和string的互相转换实现
在VC++开发中,经常会用到string和wstring,这就需要二者之间的转换,项目中封装了wstring和string相互转换的2个函数,实现如下: //将wstring转换成string std ...
- C# Byte[] 转String 无损转换
C# Byte[] 转String 无损转换 转载请注明出处 http://www.cnblogs.com/Huerye/ /// <summary> /// string 转成byte[ ...
- CString 转 char*; wstring 转 string
1. CString 转 char* ); CString name; name.Format(_T("bookUC%d.txt"),m_ID); std::wstring _n ...
- C# 之 将string数组转换到int数组并获取最大最小值
1.string 数组转换到 int 数组 " }; int[] output = Array.ConvertAll<string, int>(input, delegate(s ...
- 转:char*, char[] ,CString, string的转换
转:char*, char[] ,CString, string的转换 (一) 概述 string和CString均是字符串模板类,string为标准模板类(STL)定义的字符串类,已经纳入C++标准 ...
- HTML5 Blob与ArrayBuffer、TypeArray和字符串String之间转换
1.将String字符串转换成Blob对象 //将字符串 转换成 Blob 对象 var blob = new Blob(["Hello World!"], { type: 'te ...
- 【python】bytearray和string之间转换,用在需要处理二进制文件和数据流上
最近在用python搞串口工具,串口的数据流基本读写都要靠bytearray,而我们从pyqt的串口得到的数据都是string格式,那么我们就必须考虑到如何对这两种数据进行转换了,才能正确的对数据收发 ...
随机推荐
- django从零开始-模板
1.应用中添加模板 INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contentt ...
- bzoj4240有趣的家庭菜园(贪心+逆序对)
对家庭菜园有兴趣的JOI君每年在自家的田地中种植一种叫做IOI草的植物.JOI君的田地沿东西方向被划分为N个区域,由西到东标号为1~N.IOI草一共有N株,每个区域种植着一株.在第i个区域种植的IOI ...
- centos7 下解决mysql-server找不到安装包问题
第一步:安装从网上下载文件的wget命令 [root@master ~]# yum -y install wget 第二步:下载mysql的repo源 [root@master ~]# wget ht ...
- spring代码异常捕获到logback logging.config=logback-spring.xml文件中不能输出异常e.printStackTrace
在spring中使用logging.config=logback-spring.xml将日志转存到了文件中.但是代码中的捕获的异常无法用 e.printStackTrace 打印到文件中.使用如下方法 ...
- A1128. N Queens Puzzle
The "eight queens puzzle" is the problem of placing eight chess queens on an 8×8 chessboar ...
- 【POJ2230】Watchcow
题目大意:给定一个 N 个点,M 条边的无向图,要求不重复地经过每条边两次,并且从 1 号节点出发最后回到 1 号节点,求一条路径. 题解:不重复地经过两次这个操作很容易地通过无向图的建边方式来实现, ...
- js 判断所选时间(或者当前时间)是否在某一时间段的实现代码
var time_range = function (beginTime, endTime, nowTime) { var strb = beginTime.split (":") ...
- Java 存储时间戳的几种方式
有时需要记录一下数据生成时间的时间戳,精确到秒,这里记录一下java存储时间戳字符串的几种方式 1.DateFormat private static final SimpleDateFormat s ...
- 异步ztree 加复选框 及相应后台处理
异步加载 tree,点一下节点,就发一下请求到后台,然后显示出得到的当前层级节点 <!DOCTYPE html> <html> <head> <meta ch ...
- C实现读写文件
https://www.cnblogs.com/zhanghongfeng/p/7726199.html https://www.cnblogs.com/xudong-bupt/p/3478297.h ...