Unicode与UTF-8转换

时间:2023-01-24 19:34:07
/*
	*Unicode转换成UTF-8
	*@param strUnicode: 待转换的CString
	*@param szUtf8:转换后的UTF-8
	*@return:返回UTF-8格式的长度
	*/
	int UniToUTF8( wchar_t* pUniString, char *szUtf8 )
	{
		int nLen = WideCharToMultiByte(CP_UTF8, 0, pUniString, -1, NULL, 0, NULL, NULL); 
		char *szUtf8Temp = new char[nLen + 1];
		memset( szUtf8Temp, 0, nLen + 1 ); 
		WideCharToMultiByte ( CP_UTF8, 0, pUniString, -1, szUtf8Temp, nLen, NULL, NULL ); 
		sprintf( szUtf8, "%s", szUtf8Temp );
		delete[] szUtf8Temp;
		return nLen;
	}
	int UniToUTF8( wchar_t* pUniString, std::string& strUtf8 )
	{
		int nLen = WideCharToMultiByte(CP_UTF8, 0, pUniString, -1, NULL, 0, NULL, NULL); 
		char *szUtf8Temp = new char[nLen + 1];
		memset( szUtf8Temp, 0, nLen + 1 ); 
		WideCharToMultiByte ( CP_UTF8, 0, pUniString, -1, szUtf8Temp, nLen, NULL, NULL ); 
		strUtf8 = szUtf8Temp;
		delete[] szUtf8Temp; 
		return nLen;
	}

	/*
	*UTF-8转换成Unicode
	*@param UTF8: 待转换的的UTF-8
	*@return:返回Unicode格式的字符串
	*/
	CString UTF8ToUni( char* UTF8 )
	{
		DWORD dwUnicodeLen;        //转换后Unicode的长度
		wchar_t *pwText;            //保存Unicode的指针
		CString strUnicode;        //返回值

		//获得转换后的长度,并分配内存
		dwUnicodeLen = MultiByteToWideChar(CP_UTF8,0,UTF8,-1,NULL,0);
		pwText = new wchar_t[dwUnicodeLen];
		if (!pwText)
		{
			return strUnicode;
		}
		//转为Unicode
		MultiByteToWideChar(CP_UTF8,0,UTF8,-1,pwText,dwUnicodeLen);
		//转为CString
		strUnicode.Format(_T("%s"),pwText);
		//清除内存
		delete []pwText;
		//返回转换好的Unicode字串
		return strUnicode;
	}

	CString UTF8ToUni( std::string& strUTF8 )
	{
		DWORD dwUnicodeLen;       //转换后Unicode的长度
		wchar_t *pwText;          //保存Unicode的指针
		CString strUnicode;       //返回值

		//获得转换后的长度,并分配内存
		dwUnicodeLen = MultiByteToWideChar( CP_UTF8, 0, strUTF8.c_str(), -1, NULL, 0 );
		pwText = new wchar_t[ dwUnicodeLen ];
		if ( !pwText ){
			return strUnicode;
		}

		//转为Unicode
		MultiByteToWideChar( CP_UTF8, 0, strUTF8.c_str(), -1, pwText, dwUnicodeLen );
		strUnicode = pwText;

		//清除内存
		delete [] pwText;
		return strUnicode;
	}
<pre name="code" class="cpp">void ConvertUtf8ToGBK(CString& strUtf8)
{
	int len = MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUtf8, -1, NULL, 0);
	unsigned short* wszGBK = new unsigned short[len + 1];
	memset(wszGBK, 0, len * 2 + 2);
	MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUtf8, -1, (LPWSTR)wszGBK, len);

	len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)wszGBK, -1, NULL, 0, NULL, NULL); 
	char* szGBK = new char[len + 1];
	memset(szGBK, 0, len + 1);
	WideCharToMultiByte (CP_ACP, 0, (LPCWSTR)wszGBK, -1, szGBK, len, NULL, NULL);

	strUtf8 = szGBK;

	delete[] szGBK;
	delete[] wszGBK;
}

void ConvertGBKToUtf8(CString& strGBK) 
{
	int len = MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK, -1, NULL, 0);
	unsigned short* wszUtf8 = new unsigned short[len + 1];
	memset(wszUtf8, 0, len * 2 + 2);
	MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK, -1, (LPWSTR)wszUtf8, len);

	len = WideCharToMultiByte(CP_UTF8, 0, (LPWSTR)wszUtf8, -1, NULL, 0, NULL, NULL); 
	char* szUtf8 = new char[len + 1];
	memset(szUtf8, 0, len + 1);
	WideCharToMultiByte (CP_UTF8, 0, (LPWSTR)wszUtf8, -1, szUtf8, len, NULL,NULL);

	strGBK = szUtf8;

	delete[] szUtf8;
	delete[] wszUtf8;
}