• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            戰(zhàn)魂小筑

            討論群:309800774 知乎關(guān)注:http://zhihu.com/people/sunicdavy 開源項(xiàng)目:https://github.com/davyxu

               :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
              257 隨筆 :: 0 文章 :: 506 評(píng)論 :: 0 Trackbacks

            參考來源:http://blog.csdn.net/flying8127/article/details/1598521

            在原來原基礎(chǔ)上,將代碼整理,并加強(qiáng)安全性. 并按照WindowsAPI設(shè)計(jì), 添加輸出緩沖長度探測(cè)功能

            當(dāng)OutUTFString為NULL時(shí), 可以進(jìn)行輸出的UTF8字符串長度探測(cè)

               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:      }

             

            當(dāng)OutUnicodeString為NULL時(shí), 可以進(jìn)行輸出的Unicode字符串長度探測(cè)

             

               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:                  if ( UTF8Index + cUTF8CharRequire > UTF8StringLength )
              19:                      break;
              20:   
              21:                  if ( OutUnicodeString )
              22:                  {
              23:                      wchar_t& WideChar = OutUnicodeString[UniIndex]; 
              24:   
              25:                      WideChar = UTF8Char;
              26:                  }
              27:   
              28:                  UTF8Index++;
              29:                  
              30:              } 
              31:              else if((UTF8Char & 0xE0) == 0xC0)  ///< 110x-xxxx 10xx-xxxx
              32:              {
              33:                  const uint32 cUTF8CharRequire = 2;
              34:   
              35:                  // UTF8字碼不足
              36:                  if ( UTF8Index + cUTF8CharRequire > UTF8StringLength )
              37:                      break;
              38:   
              39:                  if ( OutUnicodeString )
              40:                  {
              41:                      wchar_t& WideChar = OutUnicodeString[UniIndex]; 
              42:                      WideChar  = (UTF8String[UTF8Index + 0] & 0x3F) << 6;
              43:                      WideChar |= (UTF8String[UTF8Index + 1] & 0x3F);
              44:                  }
              45:                  
              46:                  UTF8Index += cUTF8CharRequire;
              47:              }
              48:              else if((UTF8Char & 0xF0) == 0xE0)  ///< 1110-xxxx 10xx-xxxx 10xx-xxxx
              49:              {
              50:                  const uint32 cUTF8CharRequire = 3;
              51:   
              52:                  // UTF8字碼不足
              53:                  if ( UTF8Index + cUTF8CharRequire > UTF8StringLength )
              54:                      break;
              55:   
              56:                  if ( OutUnicodeString )
              57:                  {
              58:                      wchar_t& WideChar = OutUnicodeString[UniIndex]; 
              59:   
              60:                      WideChar  = (UTF8String[UTF8Index + 0] & 0x1F) << 12;
              61:                      WideChar |= (UTF8String[UTF8Index + 1] & 0x3F) << 6;
              62:                      WideChar |= (UTF8String[UTF8Index + 2] & 0x3F);
              63:                  }
              64:                  
              65:   
              66:                  UTF8Index += cUTF8CharRequire;
              67:              } 
              68:              else if((UTF8Char & 0xF8) == 0xF0)  ///< 1111-0xxx 10xx-xxxx 10xx-xxxx 10xx-xxxx 
              69:              {
              70:                  const uint32 cUTF8CharRequire = 4;
              71:   
              72:                  // UTF8字碼不足
              73:                  if ( UTF8Index + cUTF8CharRequire > UTF8StringLength )
              74:                      break;
              75:   
              76:                  if ( OutUnicodeString )
              77:                  {
              78:                      wchar_t& WideChar = OutUnicodeString[UniIndex]; 
              79:   
              80:                      WideChar  = (UTF8String[UTF8Index + 0] & 0x0F) << 18;
              81:                      WideChar  = (UTF8String[UTF8Index + 1] & 0x3F) << 12;
              82:                      WideChar |= (UTF8String[UTF8Index + 2] & 0x3F) << 6;
              83:                      WideChar |= (UTF8String[UTF8Index + 3] & 0x3F);
              84:                  }
              85:   
              86:                  UTF8Index += cUTF8CharRequire;
              87:              } 
              88:              else ///< 1111-10xx 10xx-xxxx 10xx-xxxx 10xx-xxxx 10xx-xxxx 
              89:              {
              90:                  const uint32 cUTF8CharRequire = 5;
              91:   
              92:                  // UTF8字碼不足
              93:                  if ( UTF8Index + cUTF8CharRequire > UTF8StringLength )
              94:                      break;
              95:   
              96:                  if ( OutUnicodeString )
              97:                  {
              98:                      wchar_t& WideChar = OutUnicodeString[UniIndex]; 
              99:   
             100:                      WideChar  = (UTF8String[UTF8Index + 0] & 0x07) << 24;
             101:                      WideChar  = (UTF8String[UTF8Index + 1] & 0x3F) << 18;
             102:                      WideChar  = (UTF8String[UTF8Index + 2] & 0x3F) << 12;
             103:                      WideChar |= (UTF8String[UTF8Index + 3] & 0x3F) << 6;
             104:                      WideChar |= (UTF8String[UTF8Index + 4] & 0x3F);
             105:                  }
             106:   
             107:                  UTF8Index += cUTF8CharRequire;
             108:              }
             109:   
             110:   
             111:              UniIndex++;
             112:          }
             113:   
             114:          return UniIndex;
             115:      }

            療效: 用了此代碼啊, 再也不用被iconv折磨了


            評(píng)論

            # re: 跨平臺(tái)Unicode與UTF8互轉(zhuǎn)代碼 2012-02-27 21:09 我要去拯救世界
            謝謝分享了!  回復(fù)  更多評(píng)論
              

            # re: 跨平臺(tái)Unicode與UTF8互轉(zhuǎn)代碼 2012-02-28 17:30 天下
            跨平臺(tái)使用挺好,
            在WIN32下,只要CW2A,CA2W宏全部搞定


              回復(fù)  更多評(píng)論
              

            # re: 跨平臺(tái)Unicode與UTF8互轉(zhuǎn)代碼 2012-03-02 10:08 pillaridge
            這個(gè)可能有點(diǎn)兒問題,wchar_t在windows下是16位,在*nix下是32位。  回復(fù)  更多評(píng)論
              

            # re: 跨平臺(tái)Unicode與UTF8互轉(zhuǎn)代碼 2012-03-02 10:13 pillaridge
            可以借用CLANG源代碼 basic下的ConvertUTF.h和ConvertUTF.c  回復(fù)  更多評(píng)論
              

            # re: 跨平臺(tái)Unicode與UTF8互轉(zhuǎn)代碼 2012-03-09 18:04 戰(zhàn)魂小筑
            @pillaridge
            感謝提醒
              回復(fù)  更多評(píng)論
              

            # re: 跨平臺(tái)Unicode與UTF8互轉(zhuǎn)代碼 2012-11-19 14:22 陳成
            100: WideChar = (UTF8String[UTF8Index + 0] & 0x07) << 24;
            101: WideChar = (UTF8String[UTF8Index + 1] & 0x3F) << 18;
            102: WideChar = (UTF8String[UTF8Index + 2] & 0x3F) << 12;
            這三行不是只有最后一行才起作用嗎?  回復(fù)  更多評(píng)論
              

            # re: 跨平臺(tái)Unicode與UTF8互轉(zhuǎn)代碼 2012-11-20 10:09 戰(zhàn)魂小筑
            @陳成
            這段代碼是從其他地方拷貝過來的  回復(fù)  更多評(píng)論
              

            # re: 跨平臺(tái)Unicode與UTF8互轉(zhuǎn)代碼 2013-06-07 17:03 zibbleR
            UniCharToUTF8 在轉(zhuǎn)換中文的時(shí)候出來的貌似還是亂碼,  回復(fù)  更多評(píng)論
              

            # re: 跨平臺(tái)Unicode與UTF8互轉(zhuǎn)代碼 2013-10-15 14:57 zozo
            關(guān)鍵是要gb2312-utf8的轉(zhuǎn)換,有木有  回復(fù)  更多評(píng)論
              

            午夜福利91久久福利| 久久综合综合久久97色| 久久综合国产乱子伦精品免费| 一本一本久久aa综合精品 | 久久久噜噜噜久久中文字幕色伊伊| 亚洲中文久久精品无码| 91精品国产综合久久香蕉| 精产国品久久一二三产区区别| 久久综合九色综合久99| 久久只有这里有精品4| 久久精品这里热有精品| 伊人久久大香线焦AV综合影院| 国产精品美女久久久网AV| 久久婷婷激情综合色综合俺也去| 久久久综合香蕉尹人综合网| 久久精品国产69国产精品亚洲| 久久免费看黄a级毛片| 久久综合视频网站| 国产2021久久精品| 99久久久精品| 久久香蕉超碰97国产精品| 国产精品成人久久久| 久久精品国产精品亚洲| 国产99久久九九精品无码| 国内精品久久久久影院免费| 久久综合给合久久狠狠狠97色69 | 久久99久久成人免费播放| 久久国产色AV免费看| 日韩av无码久久精品免费| 精品一二三区久久aaa片| 青青草原综合久久大伊人| 久久精品极品盛宴观看| 久久综合久久鬼色| 亚洲国产日韩欧美综合久久| 欧美性大战久久久久久| 久久成人精品| 欧美久久一级内射wwwwww.| 欧美一级久久久久久久大| 亚洲欧美日韩精品久久亚洲区| 久久亚洲av无码精品浪潮| 热RE99久久精品国产66热|