wchar_t* 与 std::string互相转化

最近Win32编程被编码折磨得很惨。。。记录一下两个转化函数。

wchar_t*转std::string:

std::string wchar_tToString(wchar_t *wchar){
	std::string szDst;
	wchar_t* wText = wchar;
	DWORD dwNum = WideCharToMultiByte(CP_OEMCP, NULL, wText, -1, NULL, 0, NULL, FALSE);
	char *psText;
	psText = new char[dwNum];
	WideCharToMultiByte(CP_OEMCP, NULL, wText, -1, psText, dwNum, NULL, FALSE);
	szDst = psText;
	delete[]psText;
	return szDst;
}

std::string转wchar_t*:

wchar_t* stringToWchar_t( std::string str){
	std::string temp = str;
	int len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)temp.c_str(), -1, NULL, 0);
	wchar_t *wszUtf8 = new wchar_t[len + 1];
	memset(wszUtf8, 0, len * 2 + 2);
	MultiByteToWideChar(CP_ACP , 0 , (LPCSTR)temp.c_str() , -1 , (LPWSTR)wszUtf8 , len);
	return wszUtf8;
}

 

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注