编码转换的方法(UNICODE/ASCII/UTF-8)

参考了网上一些方法:所谓的短字符,就是用8bit来表示的字符,典型的应用是ASCII码.  而宽字符,顾名思义,就是用16bit表示的字符,典型的有UNICODE.
   常用的代码页有CP_ACP和CP_UTF8两个。
   使用CP_ACP代码页就实现了ANSI与Unicode之间的转换。
   使用CP_UTF8代码页就实现了UTF-8与Unicode之间的转换。

 

1.  ASCII  to  Unicode(CP_ACP)

wstring ASCIIToUNICODE(char cArry[])        //传入参数为ANSI串,即用char数组或者string表示的串
{
  int nLen = ::MultiByteToWideChar(CP_ACP, 0, cArry, -1, NULL, NULL);   //将MultiByteToWideChar()的第四个形参设为-1,即可返回长度

  wchar_t *pTemp = new wchar_t[nLen];       //new一个wchar_t空间,保存Unicode串
  memset(pTemp, 0, nLen*sizeof(wchar_t));
  ::MultiByteToWideChar(CP_ACP, 0, cArry, -1,  (LPWSTR)pTemp, nLen);
  wstring str = pTemp;
  if (pTemp)
  {
   delete [] pTemp;
   pTemp = NULL;
  }
  return str;
}

2. Unicode to ASCII(CP_ACP)

string UNICODEToASCII(wchar_t cArry[])                   //传入参数为Unicode串,用“wchar_t cArry[] = {L"这是个测试"};”表示
{
 int nLen = ::WideCharToMultiByte(CP_ACP, 0, cArry, -1, NULL, 0, NULL, NULL);
 char *pTemp = new char[nLen];                //new 一个char数组,保存ANSI串
 memset(pTemp, 0, nLen);
 ::WideCharToMultiByte(CP_ACP, 0, cArry, -1, pTemp, nLen, NULL, NULL);
 string str = pTemp;
 if (pTemp)
 {
  delete [] pTemp;
  pTemp = NULL;
 }
 return str;
}

 

3. UTF-8 to Unicode(CP_UTF8)

wstring UTF8ToUnicode( const string& str )  

{  

    int  unicodeLen = ::MultiByteToWideChar(CP_UTF8,  0,  str.c_str(),  -1,  NULL,  0 );

    wchar_t *pUnicode = new  wchar_t[unicodeLen];

    memset(pUnicode, 0, unicodeLen*sizeof(wchar_t));

    ::MultiByteToWideChar( CP_UTF8,  0,  str.c_str(),  -1,  (LPWSTR)pUnicode,  unicodeLen );    

    wstring  rt = pUnicode;  

    if(pUnicode )

    {

         delete [] pUnicode ;

         pUnicode  = NULL;

    }   

    return  rt;    

}  

4.  Unicode to UTF-8(CP_UTF8)

string UnicodeToUTF8( const wstring& str )

{    

    // wide char to multi char   

    int iTextLen = ::WideCharToMultiByte(CP_UTF8,  0,  str.c_str(),  -1,  NULL, 0,  NULL,  NULL );  

    char *pElementText= new char[iTextLen];  

    memset(pElementText, 0, iTextLen);

    ::WideCharToMultiByte( CP_UTF8,  0,  str.c_str(),  -1,  pElementText,  iTextLen,  NULL,  NULL );  

    string strText;  

    strText = pElementText;  

    if(pElementText)

    {

        delete [] pElementText;

        pElementText = NULL;

    }

     return strText;  

}  

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值