char, wchar_t,UTF8,UNICODE,GBK转换

1 char* 转换为wchar_t

//char* to wchar_t
wchar_t wfilename[256] ={0};
char filename[] = {"c://init.properties"};
ulBytes = MultiByteToWideChar(CP_ACP,0,filename,-1,NULL,0);
ulBytes = MultiByteToWideChar(CP_ACP,0,filename,-1,wfilename,ulBytes);


2 wchar_t 转换为char*

//wchar_t to char*
ulBytes = WideCharToMultiByte(CP_ACP,0, wfilename,-1,NULL,0,NULL,NULL);
ulBytes = WideCharToMultiByte(CP_ACP,0, wfilename,-1, filename,ulBytes,NULL,NULL);


    

3 unicode to utf-8

int UniToUTF8(CString strUnicode,char *szUtf8)
{
    //MessageBox(strUnicode);
    int ilen = WideCharToMultiByte(CP_UTF8, 0, (LPCTSTR)strUnicode, -1, NULL, 0, NULL, NULL); 
    char *szUtf8Temp=new char[ilen + 1];
    memset(szUtf8Temp, 0, ilen +1); 
    WideCharToMultiByte (CP_UTF8, 0, (LPCTSTR)strUnicode, -1, szUtf8Temp, ilen, NULL,NULL); 
    //size_t a = strlen(szUtf8Temp);
    sprintf(szUtf8, "%s", szUtf8Temp);// 
    delete[] szUtf8Temp; 
    return ilen;
}


 

4. GBK to utf-8

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, wszUtf8, len);

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


5. utf-8 to GBK

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, wszGBK, len);

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


6. 读写unicode 文件。

wchar_t* ReadUFile(wchar_t* path)
{
	CStdioFileEx	fileEx;
	fileEx.SetCodePage(CP_UTF8);
	CString			sText, sLine;
	
	if(fileEx.Open(path, CFile::modeRead | CFile::typeText))
	{
		
		// Read first 15 lines
		for (short nLineCount = 0; nLineCount < 16 && fileEx.ReadString(sLine); nLineCount++)
		{ 
			sText += sLine + sNEWLINE;
		}
		fileEx.Close();
	}
	return sResult.GetBuffer(sResult.GetLength());
}


bool WriteUFile(wchar_t* path,wchar_t* content)
{
	CStdioFileEx	fileEx;
	fileEx.SetCodePage(CP_UTF8);
	CString			sText, sLine;
	UINT				nWriteFlags = CFile::modeCreate | CFile::modeWrite | CFile::typeText;
	//nWriteFlags |= CStdioFileEx::modeWriteUnicode;
	if(fileEx.Open(path, nWriteFlags))
	{
		CString scontent = content;
		fileEx.WriteString(scontent);
		fileEx.Close();
		return true;
	}
	else
	{
		return false;
	}
}


这里用到了 CStdioFileEx ,下载地址: http://files.cnblogs.com/tingsking/StdioFileEx.zip

 

7. 发送http 请求

void SendHttp()
{
	CString strData =”cardno=123456&address=张三”;

	ConvertGBKToUtf8(strData);
	try
	{
		DWORD dwServiceType = AFX_INET_SERVICE_HTTP;
		CString szServer, szObject;
		INTERNET_PORT nPort;
		CString url;
		GetDlgItemText(IDC_EDIT_ADDRESS,url);
		AfxParseURL(url, dwServiceType, szServer, szObject, nPort);
		CInternetSession Session ; 
		CHttpConnection *pHttpConnect = Session.GetHttpConnection(szServer,INTERNET_FLAG_KEEP_CONNECTION,nPort,NULL,NULL) ;
		if( pHttpConnect )
		{
			CHttpFile* pFile = pHttpConnect->OpenRequest( CHttpConnection::HTTP_VERB_POST, szObject);
			if ( pFile )
			{
				CString strHead = _T("Content-Type: application/x-www-form-urlencoded") ;
				pFile->AddRequestHeaders("Accept: */*"); 
				pFile->AddRequestHeaders("Accept-Language: zh-cn"); 
				pFile->AddRequestHeaders("Accept-Encoding: gzip"); 
				pFile->AddRequestHeaders("Accept-Charset: utf-8"); 
				ret = pFile->SendRequest( strHead ,(LPVOID)(LPCTSTR)strData ,strData.GetLength() );
				
				pFile->Close();
				delete pFile ;
			}
			
			
			pHttpConnect->Close() ;
			delete pHttpConnect ;
		}
}
	catch( CInternetException *e )
	{
		e->Delete();    
	} 
}

转载自: http://blog.csdn.net/tingsking18/article/details/5801695
http://notepad.cc

你可能感兴趣的:(char, wchar_t,UTF8,UNICODE,GBK转换)