char, wchar_t,UTF8,UNICODE,GBK转换

转载 2011年01月19日 18:02:00

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

相关文章推荐

[C/C++]_[utf8和unicode的相互转换]

场景: 1.有些库需要接收utf8字符串(也比如数据库表数据),而路径处理则需要unicode字符串,所有在windows下互相转换是必须的。 2.之前自己实现了utf8转unicode,但是并不完美...

解决你的乱码难题(Qt转码与char和utf8的互转)

在跨平台的开发中,字符串的转码显得相当重要,稍有不慎,将会出现乱码的情况,在这里,首先说一下Qt的QString中几个关于转码的函数: (1)QByteArray toUtf8() const; ...

char, wchar_t,UTF8,UNICODE,GBK转换

1 char* 转换为wchar_t [cpp] view plaincopy //char* to wchar_t  wchar_t wfilename[256] ={0};  char f...

C++中 Unicode 与 UTF-8 编码互转

1、简述最近在发送网络请求时遇到了中文字符乱码的问题,在代码中调试字符正常,用抓包工具抓的包中文字符显示正常,就是发送到服务器就显示乱码了,那就要将客户端和服务器设置统一的编码(UTF-8),而我们程...

Unicode与UTF-8互转(C语言实现)

>                 Tags: encoding,c 1. 基础 1.1 ASCII码 我们知道, 在计算机内部, 所有的信息最终都表示为一个二进制的字符串. 每一...

C++处理UTF8编码的字符串

在windows下打开一个记事本,保存文件,下面有四种编码选择: ①ANSI,也就是多字节字符集,在中文系统下约等于GB2312。 ②Unicode,就是UTF16 LE,小端序存储的UTF16。 ③...

给定A, B两个整数,不使用除法和取模运算,求A/B的商和余数

给定A, B两个整数,不使用除法和取模运算,求A/B的商和余数。 1.   最基本的算法是,从小到大遍历: for (i = 2 to A -1)          if (i * B > A)...

利用K-means聚类算法根据经纬度坐标对中国省市进行聚类

K-means聚类算法是一种非层次聚类算法,在最小误差的基础上将数据划分了特定的类,类间利用距离作为相似度指标,两个向量之间的距离越小,其相似度就越高。程序读取全国省市经纬度坐标,然后根据经纬度坐标进...

Radon变换理论介绍与matlab实现--经验交流

本人最近在研究Radon变换,在查阅了各种资料之后在此写下个人的理解,希望与各位牛牛进行交流共同进步,也使得理解更加深刻些。 Radon变换的本质是将原来的函数做了一个空间转换,即,将原来的XY平...

CT图像重建技术

由于csdn贴图不方便,并且不能上传附件,我把原文上传到了资源空间CT图像重建技术 1.引言 计算机层析成像(Computed Tomography,CT)是通过对物体进行不同角度的射线投影测量而...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:char, wchar_t,UTF8,UNICODE,GBK转换
举报原因:
原因补充:

(最多只允许输入30个字)