MFC在多字节环境下计算unicode串长度和截断中文字符

 1 CString CmuchbyteDlg::GetChinese(int size,CString &str)
 2 {
 3 
 4  DWORD dBufSize = MultiByteToWideChar(CP_ACP, 0, str, str.GetLength(), NULL, 0);
 5 
 6  wchar_t * dBuf=new wchar_t[dBufSize];
 7  //初始化
 8  wmemset(dBuf,0,dBufSize);
 9 
10  int nRet = MultiByteToWideChar(CP_ACP, 0, str,  str.GetLength(), dBuf, dBufSize);
11 
12  CString Ansstr = "";
13  int ans = 0;
14  for(int i = 0;i < nRet;i++)
15  {
16   if(dBuf[i] < 0 || dBuf[i] > 0x80)
17   {
18    if(ans >= size)
19     break;
20    ans ++;
21    CString cstring(dBuf[i]);
22    Ansstr += cstring;
23   }
24  }
25  return Ansstr;
26 }

 

posted @ 2019-10-14 09:31  冷眼旁观你的泪  阅读(370)  评论(0编辑  收藏  举报