码迷,mamicode.com
首页 > Windows程序 > 详细

win32 wstring <-> string

时间:2015-02-05 18:51:19      阅读:364      评论:0      收藏:0      [点我收藏+]

标签:

static std::string w2c(std::wstring str)
	{
		int nlength = str.length();
		int nbytes = WideCharToMultiByte(0,0,str.c_str(),nlength,NULL,0,NULL,NULL);
		if(nbytes == 0) return "";

		char*buff = new char[nbytes+1];
		WideCharToMultiByte(0,0,str.c_str(),nlength,buff,nbytes,NULL,NULL);
		buff[nbytes] = ‘\0‘;
		std::string ret_str = std::string(buff);
		delete [] buff;
		return ret_str;		
	}

static std::wstring c2w(std::string str)
	{
		if(str.length() == 0) return std::wstring();

		int nu = str.length();
		size_t n =(size_t)MultiByteToWideChar(CP_ACP,0,str.c_str(),nu,NULL,0);
		wchar_t*wbuff = new wchar_t[n+1];
		MultiByteToWideChar(CP_ACP,0,str.c_str(),(int)nu,wbuff,(int)n);
		wbuff[n] = 0;

		std::wstring wstr_ret = std::wstring(wbuff);
		delete []wbuff;
		return wstr_ret;
	}



win32 wstring <-> string

标签:

原文地址:http://my.oschina.net/lyr/blog/375799

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!