string和wstring相互转换
VS2015+QT环境,加载dll函数loadlibrary,使用宽字符参数。路径有汉字总加载失败,于是先将汉字转化再加载dll,成功。
std::string ConvertUtf8ToGbk(const char* srcStr)
{
//utf-8转换为unicode
u32 dwStrLen = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)srcStr, -1, NULL, 0);
wchar_t * pwszGBK = new wchar_t[dwStrLen];
memset(pwszGBK, 0, dwStrLen);
MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)srcStr, -1, pwszGBK, dwStrLen);
//unicode转为ansi
dwStrLen = WideCharToMultiByte(CP_ACP, 0, pwszGBK, -1, NULL, 0, NULL, NULL);
char* pchGBK = new char[dwStrLen + 1];
memset(pchGBK, 0, dwStrLen + 1);
WideCharToMultiByte(CP_ACP, 0, pwszGBK, -1, pchGBK, dwStrLen, NULL, NULL);
std::string szGbk = pchGBK;
delete[] pwszGBK;
delete[] pchGBK;
return szGbk;
}
std::wstring Ansi2WChar(LPCSTR pszSrc, int nLen)
{
int nSize = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pszSrc, nLen, 0, 0);
if (nSize <= 0) return NULL;
WCHAR *pwszDst = new WCHAR[nSize + 1];
memset(pwszDst, 0, nSize + 1);
if (NULL == pwszDst) return NULL;
MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pszSrc, nLen, pwszDst, nSize);
pwszDst[nSize] = 0;
if (pwszDst[0] == 0xFEFF) // skip Oxfeff
for (int i = 0; i < nSize; i++)
pwszDst[i] = pwszDst[i + 1];
std::wstring wcharString(pwszDst);
delete pwszDst;
return wcharString;
}
void CDllManage::LoadDll()
{
//加载univideocap.dll库
QString szPath = QCoreApplication::applicationDirPath();
szPath += "/univideocap.dll";
std::string szTemp = ConvertUtf8ToGbk(szPath.toStdString().c_str());
PrintLog(log_lv_debug, "路径: %s\n", szTemp.c_str());
std::wstring szDst = Ansi2WChar(szTemp.c_str(), szTemp.size());
if (false == szDst.empty())
{
m_hModule = LoadLibrary(szDst.c_str());
if (nullptr == m_hModule)
{
//qDebug() << "load dll fail, GetLastError:" << GetLastError() << endl;
PrintLog(log_lv_debug, "加载uniplaydll失败了:%d\n", GetLastError());
}
else
{
PrintLog(log_lv_debug, "加载成功了\n");
}
}
}
摘自:https://blog.csdn.net/layer781010/article/details/10224553
1. 或许可以试试下面这个方法//试了不行
- // std::string -> std::wstring
- std::string s("string");
- std::wstring ws;
- ws.assign(s.begin(), s.end());
- // std::wstring -> std::string
- std::wstring ws(L"wstring");
- std::string s;
- s.assign(ws.begin(), ws.end());
2.
第一种方法:调用WideCharToMultiByte()和MultiByteToWideChar(),代码如下(关于详细的解释,可以参考《windows核心编程》):
- #include <string>
- #include <windows.h>
- using namespace std;
- //Converting a WChar string to a Ansi string
- std::string WChar2Ansi(LPCWSTR pwszSrc)
- {
- int nLen = WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, NULL, 0, NULL, NULL);
- if (nLen<= 0) return std::string("");
- char* pszDst = new char[nLen];
- if (NULL == pszDst) return std::string("");
- WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, pszDst, nLen, NULL, NULL);
- pszDst[nLen -1] = 0;
- std::string strTemp(pszDst);
- delete [] pszDst;
- return strTemp;
- }
- string ws2s(wstring& inputws){ return WChar2Ansi(inputws.c_str()); }
- //Converting a Ansi string to WChar string
- std::wstring Ansi2WChar(LPCSTR pszSrc, int nLen)
- {
- int nSize = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pszSrc, nLen, 0, 0);
- if(nSize <= 0) return NULL;
- WCHAR *pwszDst = new WCHAR[nSize+1];
- if( NULL == pwszDst) return NULL;
- MultiByteToWideChar(CP_ACP, 0,(LPCSTR)pszSrc, nLen, pwszDst, nSize);
- pwszDst[nSize] = 0;
- if( pwszDst[0] == 0xFEFF) // skip Oxfeff
- for(int i = 0; i < nSize; i ++)
- pwszDst[i] = pwszDst[i+1];
- wstring wcharString(pwszDst);
- delete pwszDst;
- return wcharString;
- }
- std::wstring s2ws(const string& s){ return Ansi2WChar(s.c_str(),s.size());}
第二种方法:采用ATL封装_bstr_t的过渡:(注,_bstr_是Microsoft Specific的,所以下面代码可以在VS2005通过,无移植性);
- #include <string>
- #include <comutil.h>
- using namespace std;
- #pragma comment(lib, "comsuppw.lib")
- string ws2s(const wstring& ws);
- wstring s2ws(const string& s);
- string ws2s(const wstring& ws)
- {
- _bstr_t t = ws.c_str();
- char* pchar = (char*)t;
- string result = pchar;
- return result;
- }
- wstring s2ws(const string& s)
- {
- _bstr_t t = s.c_str();
- wchar_t* pwchar = (wchar_t*)t;
- wstring result = pwchar;
- return result;
- }
第三种方法:使用CRT库的mbstowcs()函数和wcstombs()函数,平台无关,需设定locale。
- #include <string>
- #include <locale.h>
- using namespace std;
- string ws2s(const wstring& ws)
- {
- 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);
- string result = _Dest;
- delete []_Dest;
- setlocale(LC_ALL, curLocale.c_str());
- return result;
- }
- wstring s2ws(const 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);
- wstring result = _Dest;
- delete []_Dest;
- setlocale(LC_ALL, "C");
- return result;
- }
//第四种方法,标准C++转换方法:(待续)
//第五种方法,在C++中使用C#类库:(待续
其中第四种,我的实现始终存在一些问题。第五种,我只是知道有这么一种方案,没有时间去详细了解,算是给一些提示吧。
转载于:https://www.cnblogs.com/damoying/p/10868179.html
string和wstring相互转换相关推荐
- C++排雷:19.过滤英文和中文标点符号,string与wstring之间的转换
想要过滤一个文本中的标点符号. 对英文标点符号可以使用cctype中的ispunct方法来识别 而对于中文标点符号,则需要一定的转换: C++用string来处理字符串. string是窄字符串ASC ...
- 几种C++ std::string和std::wstring相互转换的转换方法
第一种方法:调用WideCharToMultiByte()和MultiByteToWideChar(),代码如下(关于详细的解释,可以参考<windows核心编程>): #include ...
- wchar_t*,wchar_t,wchat_t数组,char,char*,char数组,std::string,std::wstring,CString....转换
wchar_t*,wchar_t,wchat_t数组,char,char*,char数组,std::string,std::wstring,CString.... #include <strin ...
- PInvoke复习之深入理解char*与wchar_t*与string以及wstring之间的相互转换
本篇文章是对c++中的char*与wchar_t*与string以及wstring之间的相互转换进行了详细的分析介绍,需要的朋友参考下 #ifndef USE_H_ #define USE_H_ #i ...
- wchar_t*,wchar_t,wchat_t数组,char,char*,char数组,std::string,std::wstring,CString 以及system(command)...
转载:http://blog.csdn.net/chocolateconanlan/article/details/4058868 wchar_t*,wchar_t,wchat_t数组,char,ch ...
- String与InputStream相互转换
1.String to InputStream String str = "String与InputStream相互转换"; InputStream in_nocode = new ...
- string、wstring、cstring、 char、 tchar、int转换方法
1.string转wstring ? 1 2 3 4 5 6 7 wstring s2ws(const string& s) { _bstr_t t = s.c_str(); wchar_ ...
- string、wstring、UTF-8、UTF-16、UTF-32之间转换
//string转wstring std::wstring string_to_wstring(const std::string& str) { setlocale(LC_ALL, &quo ...
- [转]wchar_t char std::string std::wstring CString 转换
[转]wchar_t char std::string std::wstring CString 转换 1 #include <string> 2 // 使用CString必须使用MFC, ...
最新文章
- python3 import execjs ModuleNotFoundError: No module named ‘execjs‘
- oracle客户端下载 win8.1,WINDOWS8.1安装ORACLE客户端及配置
- 根据当前日期算前一年、前一月、前一天(java基础)
- 带父节点的平衡二叉树_深入理解(二叉树、平衡二叉树、B-Tree、B+Tree )的区别
- JDBC连接sql server数据库
- 是按压还是触摸_一文读懂,选联想还是华为?Matebook 14锐龙版和YOGA 14s、小新Pro13怎么选?...
- 【入门2】分支结构 (今天刷洛谷了嘛)
- linux timespec 链接库,Linux内核 timespec_sub()
- 使用rsync无密码传输
- 多云时代-着眼布局开源技术之多云数据管理
- 数据库入门-主键和外键设置
- 《SAP后勤模块实施攻略—SAP在生产、采购、销售、物流中的应用》——3.3 MRP结果评估概览...
- 小森林顺序_电影:《小森林》两部
- python批量生成excel文件_实例1:用Python自动批量生成Excel档物料标签
- 目标检测--轻量级网络(截至2022-04-21)
- Exploitation and Exploration
- Adobe2023全家桶win及Mac系统安装包下载及安装教程ps、pr、ai、ae安装包下载
- 内心宁静_宁静js的新功能2 16 0
- 电视信号中的一些基本概念 (转自驱动之家)
- 有各组方差怎么算组间平方和_#excel单因素方差分析#excel如何计算组间离差平方和...
热门文章
- 当今将Windows应用程序迁移到Windows on Arm的实践
- Blazor 服务器上带有 EF Core 的 Azure Cosmos DB
- (八)构建一个Docker容器来训练Deep Fake Autoencoders
- 微软开源 Outlook for iOS 暗黑模式解决方案:支持 iOS 11 及更高版本
- JavaScript和Chrome中的SSDP发现
- 微信登录画面_微信登录界面改版,美炸了!
- latex设置一级标题样式不居中_Word应用“样式”的设置
- ajaxsetup获取ajax的url_跨域调用接口的方法之一:$.ajaxSetup()
- mysql5.7安装教程绿色_mysql 5.7.17 安装配置方法图文教程(windows)
- dll文件 修改特征码_Could not load dynamic library cublas64_10.dll