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

编码问题:unicode与utf-8,wchar_t与char

unicode与utf-8 wchar_t
  • freestyle4568
  • freestyle4568
  • 2015年11月02日 21:32
  • 1446

使用c++11标准库转换字符编码

#include #include #include const std::string ws2s( const std::wstring& src ) { std::locale sys_...
  • wangji163163
  • wangji163163
  • 2013年04月03日 18:38
  • 3773

Linux下转换字符集(UTF8转换)

今天在Linux 下使用 Iconv 命令转换一个UTF8文件时,总是转换不成功。提示: iconv: 未知 0 处的非法输入序列 后来使用 man iconv 查看,还是没发现异常,因为...
  • wuxinyanzi
  • wuxinyanzi
  • 2013年10月16日 14:41
  • 22086

wchar_t与char(系统默认编码集,非UTF8)的转换

最近开始看孙鑫老师的c++视频,在跟着视频做东西的时候,碰到了这个问题,视频中应该没有定义UNICODE,所以它里面的字符串就是简单用" "就行了,但是我在自己电脑上编译时,就碰到了问题,就因为创建工...
  • q408384053
  • q408384053
  • 2012年04月29日 12:30
  • 2365

简单的wchar_t 和 char 转换类, 且包含与UTF8的转换

/*************************************************************** 简单的wchar_t 和 char 转换类, 且包含...
  • sailing0123
  • sailing0123
  • 2012年03月15日 18:45
  • 2030

编码问题:unicode与utf-8,wchar_t与char

unicode与utf-8 wchar_t
  • freestyle4568
  • freestyle4568
  • 2015年11月02日 21:32
  • 1446

BIG5编码, GB编码(GB2312, GBK, ...), Unicode编码, UTF8, WideChar, MultiByte, Char 说明与区别

一. BIG5编码, GB编码(GB2312, GBK, ...), Unicode编码 1.1. BIG5编码 BIG5字集是台湾繁体字集,共包括国标繁体汉字13053个 1.2. GB编码 GB2...
  • cay22
  • cay22
  • 2014年12月31日 13:55
  • 3368

C++读取UNICODE,UTF-8,ANSI,及char wchar相互转换

根据msdn描述,如果参数为ccs=UNICODE,则 (注:这里的UNICODE指UTF-8和UTF-16LE等的统称)     如果文件没有BOM头,则编码为ANSI,相当于没有写css=  ...
  • password318
  • password318
  • 2011年11月11日 10:30
  • 3827

Java实现的utf8,gbk,unicode编码相互转换的代码

public class UTF2GBK { public String gbk2utf8(String gbk) { String l_temp = GBK2Unicode(gbk); ...
  • xyw_Eliot
  • xyw_Eliot
  • 2013年04月21日 10:43
  • 1703

不依赖任何系统API,用c语言实现gbk/utf8/unicode编码转换

汉字'我' Unicode编码是 0x6211       01100010 00010001 UTF8编码是    0xe68891    11100110 10001000 100010001 ...
  • bladeandmaster88
  • bladeandmaster88
  • 2017年02月02日 19:10
  • 1552
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:char, wchar_t,UTF8,UNICODE,GBK转换
举报原因:
原因补充:

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