L o a d i n g . . .
主打一个C++
文章详情

C++字符串指针char与wchar_t互转

Posted on 2019-07-29 20:23:45 by 主打一个C++

wchar_t* 转 char*

int wstr2str(const wchar_t* _wstr,std::string& _cstr)
{
	int len = WideCharToMultiByte(CP_ACP, 0, _wstr, -1, NULL, 0, NULL, NULL);
	char* str = new char[len + 1];
	int rlen = WideCharToMultiByte(CP_ACP, 0, _wstr, -1, str, len, NULL, NULL);
	str[len] = '\0';
	_cstr = str;
	return rlen;
}

char* 转 wchar_t*

int str2wstr(const char* _str, std::wstring& _wstr)
{
	int len = MultiByteToWideChar(CP_ACP, 0, _str, -1, NULL, 0);
	wchar_t* wstr = new wchar_t[len + 1];
	int rlen = MultiByteToWideChar(CP_ACP, 0, _str, -1, wstr, len);
	wstr[len] = L'\0';
	_wstr = wstr;
	return rlen;
}


*转载请注明出处:原文链接:https://cpp.vin/page/76.html

作者近期文章
  • 随手笔记
  • 主打一个C++   2025-01-11 20:02:01
  • 都2000000025年了。还有不能随意访问guthub的,仔细看。在国内其实是可以正常访问的,gfw并没屏蔽。这里给出其中一个简单直接的方法稳定访问。1. 随便百度一个”dn
提示
×
确定
数据库执行: 8次 总耗时: 0.01s
页面加载耗时: 



wechat +447752296473
wechat cpp-blog