ANSI編碼方式轉化為UTF-8方式


說明:

記事本txt有四種編碼方式,分別為:UTF-8、ANSI、Unicode和Unicode big endian,當進行寫操作,創建的txt編碼格式,與寫入漢字的編碼方式相同;如果寫入的漢字是不同的編碼方式,此時創建的txt中,會出現亂碼,所以需要把漢字轉化為同一編碼方式。

本文主要介紹:把漢字編碼方式,由ANSI方式轉化為UTF-8方式:

一、ANSI轉化為UTF-8程序:

  1. CString ToUTF8(const wchar_t* buffer, int len)  //返回類型為CString  
  2. {    
  3.     int size = ::WideCharToMultiByte(CP_UTF8, 0, buffer, len, NULL, 0, NULL,    
  4.             NULL);    
  5.     if (size == 0)    
  6.         return "";    
  7.     
  8.     std::string newbuffer;    
  9.     newbuffer.resize(size);    
  10.     ::WideCharToMultiByte(CP_UTF8, 0, buffer, len,    
  11.             const_cast<char*>(newbuffer.c_str()), size, NULL, NULL);    
  12.     
  13.     //如需返回string類型,直接 return newbuffer  
  14.       
  15.     TCHAR outstr[64]; //string 轉化為CString返回  
  16.     CString strTemp;  
  17.     memset(outstr, '\0'sizeof(outstr));  
  18.     memcpy(outstr,newbuffer.c_str(),newbuffer.size());  
  19.     strTemp.Format("%s",outstr);  
  20.     return strTemp;   
  21. }    

二、函數調用形式

  1. wstring text =  L"漢字";   
  2. CString strTemp = ToUTF8(text.c_str(),text.size());  






免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM