字符串是否是UTF8,UTF8汉字是否被截断?

UTF8 是以8bits1Bytes为编码的最基本单位,当然也可以有基于16bits32bits的形式,分别称为UTF16UTF32,但目前用得不多,而UTF8则被广泛应用在文件储存和网络传输中。
  编码原理
  先看这个模板:
  UCS-4 range (hex.) UTF-8 octet sequence (binary)
  0000 0000-0000 007F 0xxxxxxx
  0000 0080-0000 07FF 110xxxxx 10xxxxxx
  0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
  0001 0000-001F FFFF 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  0020 0000-03FF FFFF 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
  0400 0000-7FFF FFFF 1111110x 10xxxxxx ... 10xxxxxx
  编码步骤:
  1) 首先确定需要多少个8bits(octets)
  2) 按照上述模板填充每个octets的高位bits
  3) 把字符的bits填充至x中,字符顺序:低位高位,UTF8顺序:最后一个octet的最末位x→第一个octet最高位x
  根据UTF8编码,最多可由6个字节组成,所以UTF81-6字节编码组成
  C++代码如下:
  int IsTextUTF8(char* str,ULONGLONG length)
  {
  int i;
  DWORD nBytes=0;//UFT8可用1-6个字节编码,ASCII用一个字节
  UCHAR chr;
  BOOL bAllAscii=TRUE; //如果全部都是ASCII, 说明不是UTF-8
  for(i=0;i<length;i++)
  {
  chr= *(str+i);
  if( (chr&0x80) != 0 ) // 判断是否ASCII编码,如果不是,说明有可能是UTF-8,ASCII7位编码,但用一个字节存,最高位标记为0,o0xxxxxxx
  bAllAscii= FALSE;
  if(nBytes==0) //如果不是ASCII,应该是多字节符,计算字节数
  {
  if(chr>=0x80)
  {
  if(chr>=0xFC&&chr<=0xFD)
  nBytes=6;
  else if(chr>=0xF8)
  nBytes=5;
  else if(chr>=0xF0)
  nBytes=4;
  else if(chr>=0xE0)
  nBytes=3;
  else if(chr>=0xC0)
  nBytes=2;
  else
  {
  return FALSE;
  }
  nBytes--;
  }
  }
  else //多字节符的非首字节,应为 10xxxxxx
  {
  if( (chr&0xC0) != 0x80 )
  {
  return FALSE;
  }
  nBytes--;
  }
  }
  if( nBytes > 0 ) //违返规则
  {
  return FALSE;
  }
  if( bAllAscii ) //如果全部都是ASCII, 说明不是UTF-8
  {
  return FALSE;
  }
  return TRUE;

  }

  1. #include <string.h>
  2. #include <stdio.h>
  3.  
  4. bool IsUTF8String(const char* str, int length)
  5. {
  6.     int i = 0;
  7.     int nBytes = 0;//UTF8可用1-6个字节编码,ASCII用一个字节
  8.     unsigned char chr = 0;
  9.     bool bAllAscii = true;//如果全部都是ASCII,说明不是UTF-8
  10.  
  11.     while (< length)
  12.     {
  13.         chr = *(str + i);
  14.         if ((chr & 0x80) != 0)
  15.             bAllAscii = false;
  16.         if (nBytes == 0)//计算字节数
  17.         {
  18.             if ((chr & 0x80) != 0)
  19.             {
  20.                 while ((chr & 0x80) != 0)
  21.                 {
  22.                     chr <<= 1;
  23.                     nBytes++;
  24.                 }
  25.                 if (nBytes < 2 || nBytes > 6)
  26.                     return false;//第一个字节最少为110x xxxx
  27.                 nBytes--;//减去自身占的一个字节
  28.             }
  29.         }
  30.         else//多字节除了第一个字节外剩下的字节
  31.         {
  32.             if ((chr & 0xc0) != 0x80)
  33.                 return false;//剩下的字节都是10xx xxxx的形式
  34.             nBytes--;
  35.         }
  36.         ++i;
  37.     }
  38.     if (bAllAscii)
  39.         return false;
  40.     return nBytes == 0;
  41. }
  42.  
  43. int main()
  44. {
  45.     printf("%d\n", IsUTF8String("cc", strlen("cc")));
  46.     printf("%d\n", IsUTF8String("曹操", strlen("曹操")));
  47.  
  48.     return 0;
  49. }


int strLen_utf8(__in unsigned char* s, int nLen)

{


}

猜你喜欢

转载自blog.csdn.net/zimu2702/article/details/17173213