VC++의 UNICODE 프로젝트의 일반적인 부호화

3093 단어 nulldeletevc++
void CServerSession::CString2Char(CString str, char ch[])
{
	int wLen = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL);//  Char   
	WideCharToMultiByte(CP_ACP, 0, str, -1, ch, wLen, NULL, NULL);       // CString   char*
}

void CServerSession::ConvertUtf8ToGBK(CString& strUtf8)
{
	//UNICODE      wchar_t*  char*
	int len=MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)(LPCTSTR)strUtf8, -1, NULL,0);
	unsigned short * wszGBK = new unsigned short[len+1];
	memset(wszGBK, 0, len * 2 + 2);
	//           char*
	//MultiByteToWideChar(CP_UTF8, 0,(LPCTSTR)strUtf8, -1, (LPWSTR)wszGBK, len);
	MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)(LPCTSTR)strUtf8, -1, (LPWSTR)wszGBK, len);

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

	strUtf8 = szGBK;
	delete[] szGBK;
	delete[] wszGBK;
}

void CServerSession::ConvertGBKToUtf8(CString& strGBK)
{
	int len=MultiByteToWideChar(CP_ACP, 0, (LPCSTR)(LPCTSTR)strGBK, -1, NULL,0);
	unsigned short * wszUtf8 = new unsigned short[len+1];
	memset(wszUtf8, 0, len * 2 + 2);
	MultiByteToWideChar(CP_ACP, 0, (LPCSTR)(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;
}

char *CServerSession::UnicodeToUtf8(CString unicode)
{
	int u8Len = ::WideCharToMultiByte(CP_UTF8, NULL, unicode, (DWORD)wcslen(unicode), NULL, 0, NULL, NULL);
	//  ,      '\0'    
	//UTF8   Unicode     ,         ,     char     
	char* szU8 = new char[u8Len + 1];
	//  
	//unicode    strlen wcslen
	::WideCharToMultiByte(CP_UTF8, NULL, unicode, (DWORD)wcslen(unicode), szU8, u8Len, NULL, NULL);
	//    '\0'
	szU8[u8Len] = '\0';
	return szU8;
}

char *CServerSession::UnicodeToAscii(CString unicode)
{
	int ansiLen = ::WideCharToMultiByte(CP_ACP, NULL, unicode, (DWORD)wcslen(unicode), NULL, 0, NULL, NULL);
	//  ,      '\0'    
	char* szAnsi = new char[ansiLen + 1];
	//  
	//unicode    strlen wcslen
	::WideCharToMultiByte(CP_ACP, NULL, unicode, (DWORD)wcslen(unicode), szAnsi, ansiLen, NULL, NULL);
	//    '\0'
	szAnsi[ansiLen] = '\0';
	return szAnsi;
}

/*
LPCSTR    const   char* 
LPCTSTR    const   wchar_t   * 
*/

CString CServerSession::UTF8ToUnicode(char* UTF8)
{
	DWORD dwUnicodeLen;        //   Unicode   
	TCHAR *pwText;            //  Unicode   
	CString strUnicode;        //   
	//        ,     
	dwUnicodeLen = MultiByteToWideChar(CP_UTF8,0,UTF8,-1,NULL,0);
	pwText = new TCHAR[dwUnicodeLen];
	if (!pwText){
		return strUnicode;
	}
	//  Unicode
	MultiByteToWideChar(CP_UTF8,0,UTF8,-1,(LPWSTR)pwText,dwUnicodeLen);
	//  CString
	strUnicode.Format(_T("%s"),pwText);
	//    
	delete []pwText;
	//      Unicode  
	return strUnicode;
}

좋은 웹페이지 즐겨찾기