- 注册时间
 - 2021-4-16
 
- 最后登录
 - 2024-4-2
 
- 在线时间
 - 3 小时
 
 
 
 
 
编程入门 
  
	- 龙马币
 - 72 
 
 
 
 
 | 
 
 
C++ ansi转utf8、utf8转ansi相互转换 
 
 
- void Convert(const char* strIn,char* strOut, int sourceCodepage, int targetCodepage)
 
 - {
 
 -     int         len         = lstrlenA(strIn);
 
 -     int         unicodeLen  = MultiByteToWideChar(sourceCodepage, 0, strIn, -1, NULL, 0);
 
 -     wchar_t     pUnicode[1024] = {0};
 
 -     MultiByteToWideChar(sourceCodepage, 0, strIn, - 1, (LPWSTR)pUnicode, unicodeLen);
 
 -     BYTE        pTargetData[2048]   = {0};
 
 -     int         targetLen           = WideCharToMultiByte(targetCodepage, 0, (LPWSTR)pUnicode, -1, (char*)pTargetData,0, NULL, NULL);
 
 -     WideCharToMultiByte(targetCodepage, 0, (LPWSTR)pUnicode, -1,(char*)pTargetData, targetLen, NULL, NULL);
 
 -     lstrcpyA(strOut,(char*)pTargetData);
 
 - }
 
  
- void UTF82Ansi(PCHAR Src, PCHAR Dst)
 
 - {
 
 -     Convert(Src,Dst,CP_UTF8,CP_ACP);
 
 - }
 
  
- void Ansi2UTF8(PCHAR Src, PCHAR Dst)
 
 - {
 
 -     Convert(Src,Dst,CP_ACP, CP_UTF8);
 
 - }
 
  
- void TestFunc()
 
 - {
 
 -      // ansi转utf8
 
 -     char* ansi_str = "ansi_str ansi字符串";
 
 -     char utf_buf[MAX_PATH]   = {0};
 
 -     Ansi2UTF8(ansi_str, utf_buf);
 
 -  
 
 -     // utf8转ansi
 
 -     char ansi_buf[MAX_PATH]  = {0};
 
 -     UTF82Ansi(utf_buf, ansi_buf);
 
 - }
 
 
  复制代码 
 
 |   
 
 
 
 |