linqy的个人博客分享 http://blog.sciencenet.cn/u/linqy

博文

c++ 中string、UTF8、char, wchar_t,UTF8,UNICODE,GBK转换

已有 11589 次阅读 2017-8-29 14:47 |个人分类:C++|系统分类:科研笔记| 编码格式转换

普通sting类型 转UTF-8编码格式字符串

   std::string ofDewarServer::string_To_UTF8(const std::string & str)  
   {  
   int nwLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);  
     
   wchar_t * pwBuf = new wchar_t[nwLen + 1];//一定要加1,不然会出现尾巴  
   ZeroMemory(pwBuf, nwLen * 2 + 2);  
     
   ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), pwBuf, nwLen);  
     
   int nLen = ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, -1, NULL, NULL, NULL, NULL);  
     
   char * pBuf = new char[nLen + 1];  
   ZeroMemory(pBuf, nLen + 1);  
     
   ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);  
     
   std::string retStr(pBuf);  
     
   delete []pwBuf;  
   delete []pBuf;  
     
   pwBuf = NULL;  
   pBuf = NULL;  
     
   return retStr;  
   }  
   //////////////////////////////////////////////////////////////////////////  

UTF-8编码格式字符串  转普通sting类型

   std::string ofDewarServer::UTF8_To_string(const std::string & str)  
   {  
   int nwLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);  
     
   wchar_t * pwBuf = new wchar_t[nwLen + 1];//一定要加1,不然会出现尾巴  
   memset(pwBuf, 0, nwLen * 2 + 2);  
     
   MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), pwBuf, nwLen);  
     
   int nLen = WideCharToMultiByte(CP_ACP, 0, pwBuf, -1, NULL, NULL, NULL, NULL);  
     
   char * pBuf = new char[nLen + 1];  
   memset(pBuf, 0, nLen + 1);  
     
   WideCharToMultiByte(CP_ACP, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);  
     
   std::string retStr = pBuf;  
     
   delete []pBuf;  
   delete []pwBuf;  
     
   pBuf = NULL;  
   pwBuf = NULL;  
     
   return retStr;  
   }  
   //////////////////////////////////////////////////////////////////////////

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);
//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

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
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
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;
}

参考:http://blog.csdn.net/yinshi_blog/article/details/6731809  



https://blog.sciencenet.cn/blog-3134052-1073272.html

上一篇:window下安装cygwin
下一篇:关于C语言中NULL, ‘\0’ ,‘0’, “0” ,0之间的区别
收藏 IP: 124.207.244.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-28 02:12

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部