当前位置:编程学习 > C/C++ >>

跨平台Unicode与UTF8互转代码

在原来原基础上,将代码整理,并加强安全性. 并按照WindowsAPI设计, 添加输出缓冲长度探测功能

当OutUTFString为NULL时, 可以进行输出的UTF8字符串长度探测

   1:  uint32 UniCharToUTF8(wchar_t UniChar, char *OutUTFString)
   2:      {
   3:  
   4:          uint32 UTF8CharLength = 0;
   5:  
   6:          if (UniChar < 0x80)
   7:          { 
   8:              if ( OutUTFString )
   9:                  OutUTFString[UTF8CharLength++] = (char)UniChar;
  10:              else
  11:                  UTF8CharLength++;
  12:          }
  13:          else if(UniChar < 0x800)
  14:          {
  15:              if ( OutUTFString )
  16:              {
  17:                  OutUTFString[UTF8CharLength++] = 0xc0 | ( UniChar >> 6 );
  18:                  OutUTFString[UTF8CharLength++] = 0x80 | ( UniChar & 0x3f );
  19:              }
  20:              else
  21:              {
  22:                  UTF8CharLength += 2;
  23:              }
  24:          }
  25:          else if(UniChar < 0x10000 )
  26:          {
  27:              if ( OutUTFString )
  28:              {
  29:                  OutUTFString[UTF8CharLength++] = 0xe0 | ( UniChar >> 12 );
  30:                  OutUTFString[UTF8CharLength++] = 0x80 | ( (UniChar >> 6) & 0x3f );
  31:                  OutUTFString[UTF8CharLength++] = 0x80 | ( UniChar & 0x3f );
  32:              }
  33:              else
  34:              {
  35:                  UTF8CharLength += 3;
  36:              }
  37:          }
  38:          else if( UniChar < 0x200000 )
  39:          {
  40:              if ( OutUTFString )
  41:              {
  42:                  OutUTFString[UTF8CharLength++] = 0xf0 | ( (int)UniChar >> 18 );
  43:                  OutUTFString[UTF8CharLength++] = 0x80 | ( (UniChar >> 12) & 0x3f );
  44:                  OutUTFString[UTF8CharLength++] = 0x80 | ( (UniChar >> 6) & 0x3f );
  45:                  OutUTFString[UTF8CharLength++] = 0x80 | ( UniChar & 0x3f );
  46:              }
  47:              else
  48:              {
  49:                  UTF8CharLength += 4;
  50:              }
  51:  
  52:          }
  53:  
  54:          return UTF8CharLength;
  55:      }
 

当OutUnicodeString为NULL时, 可以进行输出的Unicode字符串长度探测

 

   1:  uint32 UTF8StrToUnicode( const char* UTF8String, uint32 UTF8StringLength, wchar_t* OutUnicodeString, uint32 UnicodeStringBufferSize )
   2:      {
   3:          uint32 UTF8Index = 0;
   4:          uint32 UniIndex = 0;
   5:  
   6:          while ( UTF8Index < UTF8StringLength )
   7:          {
   8:              unsigned char UTF8Char = UTF8String[UTF8Index];
   9:  
  10:              if ( UnicodeStringBufferSize != 0 && UniIndex >= UnicodeStringBufferSize )
  11:                  break;
  12:  
  13:              if ((UTF8Char & 0x80) == 0)
  14:              {
  15:                  const uint32 cUTF8CharRequire = 1;
  16:  
  17:                  // UTF8字码不足
  18:          &nb

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,