关闭

char, wchar_t,UTF8,UNICODE,GBK转换

标签: nulldeletepathinterneturlsession
6778人阅读 评论(0) 收藏 举报
分类:

1 char* 转换为wchar_t


view plaincopy to clipboardprint?
//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); 
//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*


view plaincopy to clipboardprint?
//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); 
//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


view plaincopy to clipboardprint?
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;  

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


view plaincopy to clipboardprint?
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;  

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


view plaincopy to clipboardprint?
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;  

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 文件。


view plaincopy to clipboardprint?
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;  
    }  

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 请求


view plaincopy to clipboardprint?
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();      
    }   

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/archive/2010/08/10/5801695.aspx

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:419477次
    • 积分:5128
    • 等级:
    • 排名:第5618名
    • 原创:57篇
    • 转载:168篇
    • 译文:4篇
    • 评论:55条
    最新评论
    directx
    开源引擎