VC驿站

 找回密码
 加入驿站

QQ登录

只需一步,快速开始

搜索
查看: 1875|回复: 3

[交流] 字符串转换

[复制链接]
64_avatar_middle
最佳答案
0 
在线会员 发表于 2022-6-29 17:13:28 | 显示全部楼层 |阅读模式
字符串转换功能有人有写好的代码吗?




上一篇:从实例中学C语言 实例3_1( //3_1.cpp : 百灯判亮)
下一篇:C++返回值
97_avatar_middle
最佳答案
2 
在线会员 发表于 2022-7-3 10:13:27 | 显示全部楼层
这个很全了 宽字节转窄字节 字符串相互转换之类的


  1. string GBKToUTF8(const std::string & strGBK)
  2. {
  3.         string strOutUTF8 = "";
  4.         WCHAR * str1;
  5.         int n = MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, NULL, 0);
  6.         str1 = new WCHAR[n];
  7.         MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, str1, n);
  8.         n = WideCharToMultiByte(CP_UTF8, 0, str1, -1, NULL, 0, NULL, NULL);
  9.         char * str2 = new char[n];
  10.         WideCharToMultiByte(CP_UTF8, 0, str1, -1, str2, n, NULL, NULL);
  11.         strOutUTF8 = str2;
  12.         delete[]str1;
  13.         str1 = NULL;
  14.         delete[]str2;
  15.         str2 = NULL;
  16.         return strOutUTF8;
  17. }

  18. string UTF8ToGBK(const std::string & strUTF8)
  19. {
  20.         string strOutGBK = "";
  21.         WCHAR * str1;
  22.         int n = MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, NULL, 0);
  23.         str1 = new WCHAR[n];
  24.         MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, str1, n);
  25.         n = WideCharToMultiByte(CP_ACP, 0, str1, -1, NULL, 0, NULL, NULL);
  26.         char * str2 = new char[n];
  27.         WideCharToMultiByte(CP_ACP, 0, str1, -1, str2, n, NULL, NULL);
  28.         strOutGBK = str2;
  29.         delete[]str1;
  30.         str1 = NULL;
  31.         delete[]str2;
  32.         str2 = NULL;
  33.         return strOutGBK;
  34. }

  35. wstring UTF8ToGBKW(const std::string & strUTF8)
  36. {
  37.         wstring wstrOutGBK = L"";
  38.         WCHAR * str1;
  39.         int n = MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, NULL, 0);
  40.         str1 = new WCHAR[n];
  41.         MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, str1, n);
  42.         n = WideCharToMultiByte(CP_ACP, 0, str1, -1, NULL, 0, NULL, NULL);       
  43.         wstrOutGBK = str1;
  44.         delete[]str1;
  45.         str1 = NULL;
  46.         return wstrOutGBK;
  47. }

  48. int hexCharToInt(char c)  
  49. {   
  50.         if (c >= '0' && c <= '9') return (c - '0');  
  51.         if (c >= 'A' && c <= 'F') return (c - 'A' + 10);  
  52.         if (c >= 'a' && c <= 'f') return (c - 'a' + 10);  
  53.         return 0;  
  54. }

  55. bool ReadFileData(const char* cFilePath,string& strdata)
  56. {
  57.         filebuf *pbuf=NULL;  
  58.         ifstream filestr;  
  59.         long size=0;            
  60.         filestr.open (cFilePath, ios::binary);
  61.         if( filestr.is_open() )
  62.         {
  63.                 //获取文件大小  
  64.                 pbuf=filestr.rdbuf();         
  65.                 size=pbuf->pubseekoff (0,ios::end,ios::in);  
  66.                 pbuf->pubseekpos (0,ios::in);  
  67.                 // 获取文件内容  
  68.                 size+=1;
  69.                 char * pdata=new char[size];  
  70.                 memset(pdata,0,size);       
  71.                 streamsize result=pbuf->sgetn (pdata,size);
  72.                 filestr.close();       
  73.                 strdata=string(pdata);               
  74.                 //utf-8 txt格式头0xef,0xbb,0xbf
  75.                 char cHead1=pdata[0];
  76.                 char cHead2=pdata[1];
  77.                 char cHead3=pdata[2];
  78.                 char cHeadUtf81=0xef;
  79.                 char cHeadUtf82=0xbb;
  80.                 char cHeadUtf83=0xbf;
  81.                 if( size >= 3 &&  cHead1 == cHeadUtf81 && cHead2 == cHeadUtf82 && cHead3 == cHeadUtf83 )
  82.                 {
  83.                         string strUtf8=strdata;
  84.                         strdata=strUtf8.substr(3,strUtf8.size()-3);       
  85.                 }               
  86.                 delete []pdata;
  87.                 return true;
  88.         }
  89.         return false;
  90. }

  91. long ReadFileData(const char* cFilePath,char *&pdata)
  92. {
  93.         filebuf *pbuf=NULL;  
  94.         ifstream filestr;  
  95.         long lsize=0;            
  96.         filestr.open (cFilePath, ios::binary);  
  97.         if( filestr.is_open() )
  98.         {                 
  99.                 pbuf=filestr.rdbuf();  
  100.                 lsize=pbuf->pubseekoff (0,ios::end,ios::in);  
  101.                 pbuf->pubseekpos (0,ios::in);
  102.                 pdata=new char[lsize];  
  103.                 memset(pdata,0,lsize);
  104.                 streamsize result=pbuf->sgetn (pdata,lsize);
  105.                 filestr.close();
  106.         }
  107.         return lsize;
  108. }

  109. std::wstring StringToWString(const std::string &str)
  110. {
  111.         std::wstring wstr(str.length(),L' ');
  112.         std::copy(str.begin(), str.end(), wstr.begin());
  113.         return wstr;
  114. }

  115. std::string WStringToString(const std::wstring &wstr)
  116. {
  117.         std::string str(wstr.length(), ' ');
  118.         std::copy(wstr.begin(), wstr.end(), str.begin());
  119.         return str;
  120. }

  121. string ws2s(const std::wstring& ws)
  122. {
  123.         std::string curLocale = setlocale(LC_ALL, NULL);        // curLocale = "C";
  124.         setlocale(LC_ALL, "chs");
  125.         const wchar_t* _Source = ws.c_str();
  126.         size_t _Dsize = 2 * ws.size() + 1;
  127.         char *_Dest = new char[_Dsize];
  128.         memset(_Dest,0,_Dsize);
  129.         wcstombs(_Dest,_Source,_Dsize);
  130.         std::string result = _Dest;
  131.         delete []_Dest;
  132.         setlocale(LC_ALL, curLocale.c_str());
  133.         return result;
  134. }

  135. std::wstring s2ws(const std::string& s)
  136. {
  137.         setlocale(LC_ALL, "chs");
  138.         const char* _Source = s.c_str();
  139.         size_t _Dsize = s.size() + 1;
  140.         wchar_t *_Dest = new wchar_t[_Dsize];
  141.         wmemset(_Dest, 0, _Dsize);
  142.         mbstowcs(_Dest,_Source,_Dsize);
  143.         std::wstring result = _Dest;
  144.         delete []_Dest;
  145.         setlocale(LC_ALL, "C");
  146.         return result;
  147. }

  148. void Utf8ToString(string strUtf8, string & strAnsi)
  149. {
  150.         wstring wstr;
  151.         int len = MultiByteToWideChar(CP_UTF8,0,strUtf8.c_str(),strUtf8.size(),NULL,0);
  152.         wstr.resize(len);
  153.         MultiByteToWideChar(CP_UTF8,0,strUtf8.c_str(),strUtf8.size(),&wstr[0],len);
  154.         len=WideCharToMultiByte(CP_ACP,0,wstr.c_str(),wstr.size(),NULL,0,NULL,NULL);       
  155.         strAnsi.resize(len);
  156.         WideCharToMultiByte(CP_ACP,0,wstr.c_str(),wstr.size(),&strAnsi[0],len,NULL,NULL);
  157. }

  158. void Utf8ToWString(wstring& wstr,const char* pUTF8Text)  
  159. {  
  160.         if (NULL == pUTF8Text)  
  161.         {  
  162.                 return;  
  163.         }  
  164.         int  unicodeLen = ::MultiByteToWideChar( CP_UTF8,  0,  pUTF8Text,  -1,  NULL,  0 );

  165.         wchar_t*  pUnicode = new  wchar_t[unicodeLen+1];  
  166.         if (NULL == pUnicode)  
  167.         {  
  168.                 return;  
  169.         }  
  170.         MultiByteToWideChar( CP_UTF8, 0, pUTF8Text, -1, (LPWSTR)pUnicode, unicodeLen );
  171.         wstr = pUnicode;  
  172.         delete []pUnicode;  
  173. }  

  174. char* WStringToUtf8(const wstring& wstr)  
  175. {  
  176.         char *buffer = NULL;  
  177.         int  length = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, NULL, 0, NULL, NULL);  
  178.         if (length <= 0)  
  179.         {  
  180.                 return NULL;  
  181.         }  
  182.         buffer = new char[length];  
  183.         if (buffer == NULL)  
  184.         {  
  185.                 return NULL;  
  186.         }  
  187.         ZeroMemory(buffer, length);  
  188.         WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, buffer, length, NULL, NULL);  
  189.         return buffer;  
  190. }  


  191. wchar_t * ucTowc(const unsigned char * pUC)  
  192. {  
  193.         // get the length  
  194.         int nCharacters = 0; int nIndex = 0;  
  195.         while (pUC[nIndex] != 0)  
  196.         {  
  197.                 if ((pUC[nIndex] & 0x80) == 0)  
  198.                         nIndex += 1;  
  199.                 else if ((pUC[nIndex] & 0xE0) == 0xE0)  
  200.                         nIndex += 3;  
  201.                 else  
  202.                         nIndex += 2;  

  203.                 nCharacters += 1;  
  204.         }  
  205.         // make a UTF-16 string  
  206.         wchar_t * pWC = new wchar_t [nCharacters + 1];  
  207.         nIndex = 0; nCharacters = 0;  
  208.         while (pUC[nIndex] != 0)  
  209.         {  
  210.                 if ((pUC[nIndex] & 0x80) == 0)  
  211.                 {  
  212.                         pWC[nCharacters] = pUC[nIndex];  
  213.                         nIndex += 1;  
  214.                 }  
  215.                 else if ((pUC[nIndex] & 0xE0) == 0xE0)  
  216.                 {  
  217.                         pWC[nCharacters] = ((pUC[nIndex] & 0x1F) << 12) | ((pUC[nIndex + 1] & 0x3F) << 6) | (pUC[nIndex + 2] & 0x3F);  
  218.                         nIndex += 3;  
  219.                 }  
  220.                 else  
  221.                 {  
  222.                         pWC[nCharacters] = ((pUC[nIndex] & 0x3F) << 6) | (pUC[nIndex + 1] & 0x3F);  
  223.                         nIndex += 2;  
  224.                 }  

  225.                 nCharacters += 1;  
  226.         }  
  227.         pWC[nCharacters] = 0;  

  228.         return pWC;   
  229. }

  230. string replace(const char *pszSrc, const char *pszOld, const char *pszNew)
  231. {
  232.         std::string strContent, strTemp;
  233.         strContent.assign( pszSrc );
  234.         std::string::size_type nPos = 0;
  235.         while( true )
  236.         {
  237.                 nPos = strContent.find(pszOld, nPos);
  238.                 strTemp = strContent.substr(nPos+strlen(pszOld), strContent.length());
  239.                 if ( nPos == std::string::npos )
  240.                 {
  241.                         break;
  242.                 }
  243.                 strContent.replace(nPos,strContent.length(), pszNew );
  244.                 strContent.append(strTemp);
  245.                 nPos +=strlen(pszNew) - strlen(pszOld)+1; //防止重复替换 避免死循环
  246.         }
  247.         return strContent;
  248. }
复制代码
86_avatar_middle
最佳答案
0 
在线会员 发表于 2022-7-5 20:02:04 | 显示全部楼层
感谢大佬分享
42_avatar_middle
最佳答案
0 
在线会员 发表于 2023-1-8 12:43:14 | 显示全部楼层
是有例子吗?太好了
您需要登录后才可以回帖 登录 | 加入驿站 qq_login

本版积分规则

×【发帖 友情提示】
1、请回复有意义的内容,请勿恶意灌水;
2、纯数字、字母、表情等无意义的内容系统将自动删除;
3、若正常回复后帖子被自动删除,为系统误删的情况,请重新回复其他正常内容或等待管理员审核通过后会自动发布;
4、感谢您对VC驿站一如既往的支持,谢谢合作!

关闭

站长提醒上一条 /2 下一条

QQ|小黑屋|手机版|VC驿站 ( 辽ICP备09019393号-4 )|网站地图wx_jqr

GMT+8, 2023-12-1 22:54

Powered by CcTry.CoM

© 2009-2021 cctry.com

快速回复 返回顶部 返回列表