判断一行字符串中是否包含半个中文(包含Unicode总数 为奇数)

来源:互联网 发布:工作不用java框架 编辑:程序博客网 时间:2024/06/11 06:41

bool IsHalfUnicode(CString strText)
{
  const char* szStrText = (LPCTSTR)strText;
  LPCTSTR  p=  szStrText;
 
  int nCount = 0;
  int nFonts = strText.GetLength();
  int nUnicode = 0;

  //是否为偶数个数
  int nEven = nFonts%2;
  bool bResult = false;

  while( nFonts)
  {  
   if( p[0] > 0 && p[0] <= 127)  
   {  
    if( p[0] >= 0x30 && p[0] <= 0x39)  
    {  
       //是数字  
    ++nCount;
    }  
    else if( (p[0] >= 0x41 && p[0] <= 0x5a) || (p[0] >= 0x61 && p[0] <= 0x7a) ) 
    {  
      //是字母
   ++nCount;
    }  
   }
   //双字节
   else
   {
         ++nUnicode;
   }

   ++p;
   --nFonts;
  }

  //数据全部为ascil
  if (nCount == nFonts)
  {
      bResult = true;
  }
  //数据全部为unicode
  else if (nFonts == nUnicode)
  {
      if ( nEven != 0)
   bResult = false;
  }
  //混合
  else
  {
   if (nUnicode %2)
    bResult = true;
  }

   return bResult;