开启辅助访问 切换到宽版

精易论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

用微信号发送消息登录论坛

新人指南 邀请好友注册 - 我关注人的新帖 教你赚取精币 - 每日签到


求职/招聘- 论坛接单- 开发者大厅

论坛版规 总版规 - 建议/投诉 - 应聘版主 - 精华帖总集 积分说明 - 禁言标准 - 有奖举报

查看: 2974|回复: 5
收起左侧

[易语言软件开源] 【汇编】记事本中用到的字符识别(UTF8/UNICODE)

[复制链接]
结帖率:100% (2/2)
发表于 2010-1-2 09:34:28 | 显示全部楼层 |阅读模式   山西省吕梁市
代码是MS记事本中用的字符识别代码
可以有效的识别UTF8或者是UNICODE

第一步是判断存储的文件头,如果没有文件头,则判断字符范围


  1. /* IsTextUTF8
  2. *
  3. * UTF-8 is the encoding of Unicode based on Internet Society RFC2279
  4. *
  5. * Basicly:
  6. * 0000 0000-0000 007F - 0xxxxxxx  (ascii converts to 1 octet!)
  7. * 0000 0080-0000 07FF - 110xxxxx 10xxxxxx    ( 2 octet format)
  8. * 0000 0800-0000 FFFF - 1110xxxx 10xxxxxx 10xxxxxx (3 octet format)
  9. * (this keeps going for 32 bit unicode)
  10. *
  11. *
  12. * Return value:  TRUE, if the text is in UTF-8 format.
  13. *                FALSE, if the text is not in UTF-8 format.
  14. *                We will also return FALSE is it is only 7-bit ascii, so the right code page
  15. *                will be used.
  16. *
  17. *                Actually for 7 bit ascii, it doesn't matter which code page we use, but
  18. *                notepad will remember that it is utf-8 and "save" or "save as" will store
  19. *                the file with a UTF-8 BOM.  Not cool.
  20. */


  21. INT IsTextUTF8( LPSTR lpstrInputStream, INT iLen )
  22. {
  23.     INT   i;
  24.     DWORD cOctets;  // octets to go in this UTF-8 encoded character
  25.     UCHAR chr;
  26.     BOOL  bAllAscii= TRUE;

  27.     cOctets= 0;
  28.     for( i=0; i < iLen; i++ ) {
  29.         chr= *(lpstrInputStream+i);

  30.         if( (chr&0x80) != 0 ) bAllAscii= FALSE;

  31.         if( cOctets == 0 )  {
  32.             //
  33.             // 7 bit ascii after 7 bit ascii is just fine.  Handle start of encoding case.
  34.             //
  35.             if( chr >= 0x80 ) {  
  36.                //
  37.                // count of the leading 1 bits is the number of characters encoded
  38.                //
  39.                do {
  40.                   chr <<= 1;
  41.                   cOctets++;
  42.                }
  43.                while( (chr&0x80) != 0 );

  44.                cOctets--;                        // count includes this character
  45.                if( cOctets == 0 ) return FALSE;  // must start with 11xxxxxx
  46.             }
  47.         }
  48.         else {
  49.             // non-leading bytes must start as 10xxxxxx
  50.             if( (chr&0xC0) != 0x80 ) {
  51.                 return FALSE;
  52.             }
  53.             cOctets--;                           // processed another octet in encoding
  54.         }
  55.     }

  56.     //
  57.     // End of text.  Check for consistency.
  58.     //

  59.     if( cOctets > 0 ) {   // anything left over at the end is an error
  60.         return FALSE;
  61.     }

  62.     if( bAllAscii ) {     // Not utf-8 if all ascii.  Forces caller to use code pages for conversion
  63.         return FALSE;
  64.     }

  65.     return TRUE;
  66. }


  67. /* IsInputTextUnicode
  68. * Verify if the input stream is in Unicode format.
  69. *
  70. * Return value:  TRUE, if the text is in Unicode format.
  71. *
  72. * 29 June 1998         
  73. */


  74. INT IsInputTextUnicode  (LPSTR lpstrInputStream, INT iLen)
  75. {
  76.     INT  iResult= ~0; // turn on IS_TEXT_UNICODE_DBCS_LEADBYTE
  77.     BOOL bUnicode;

  78.     // We would like to check the possibility
  79.     // of IS_TEXT_UNICODE_DBCS_LEADBYTE.
  80.     //

  81.     bUnicode= IsTextUnicode( lpstrInputStream, iLen, &iResult);

  82.     if (bUnicode                                         &&
  83.        ((iResult & IS_TEXT_UNICODE_STATISTICS)    != 0 ) &&
  84.        ((iResult & (~IS_TEXT_UNICODE_STATISTICS)) == 0 )    )
  85.     {
  86.         CPINFO cpiInfo;
  87.         CHAR* pch= (CHAR*)lpstrInputStream;
  88.         INT  cb;

  89.         //
  90.         // If the result depends only upon statistics, check
  91.         // to see if there is a possibility of DBCS.
  92.         // Only do this check if the ansi code page is DBCS
  93.         //

  94.         GetCPInfo( CP_ACP, &cpiInfo);

  95.         if( cpiInfo.MaxCharSize > 1 )
  96.         {
  97.             for( cb=0; cb<iLen; cb++ )
  98.             {
  99.                 if( IsDBCSLeadByte(*pch++) )
  100.                 {
  101.                     return FALSE;
  102.                 }
  103.             }
  104.         }
  105.      }

  106.      return bUnicode;
  107. }


  108. #define UNICODE_FFFF              0xFFFF
  109. #define REVERSE_BYTE_ORDER_MARK   0xFFFE
  110. #define BYTE_ORDER_MARK           0xFEFF


  111.     lpBuf= MapViewOfFile( hMap, FILE_MAP_READ, 0,0,len);

  112.     lpBufAfterBOM= (LPSTR) lpBuf;
  113.     if( typeFlag == FT_UNKNOWN )
  114.     {
  115.         switch(*lpBuf)
  116.         {
  117.         case BYTE_ORDER_MARK:
  118.             bUnicode= TRUE;
  119.             ftOpenedAs= FT_UNICODE;

  120.             // don't count the BOM.
  121.             nChars= len / sizeof(TCHAR) -1;
  122.             break;

  123.         case REVERSE_BYTE_ORDER_MARK:
  124.             bUnicode= TRUE;
  125.             ftOpenedAs= FT_UNICODEBE;

  126.             // don't count the BOM.
  127.             nChars= len / sizeof(TCHAR) -1;
  128.             break;

  129.         // UTF bom has 3 bytes; if it doesn't have UTF BOM just fall through ..
  130.         case BOM_UTF8_HALF:            
  131.             if (len > 2 && ((BYTE) *(((LPSTR)lpBuf)+2) == BOM_UTF8_2HALF) )
  132.             {
  133.                 bUTF8= TRUE;
  134.                 cpTemp= CP_UTF8;
  135.                 ftOpenedAs= FT_UTF8;
  136.                 // Ignore the first three bytes.
  137.                 lpBufAfterBOM= (LPSTR)lpBuf + 3;
  138.                 len -= 3;
  139.                 break;
  140.             }

  141.         default:

  142.             // Is the file unicode without BOM ?
  143.             if ((bUnicode= IsInputTextUnicode((LPSTR) lpBuf, len)))
  144.             {
  145.                 ftOpenedAs= FT_UNICODE;
  146.                 nChars= len / sizeof(TCHAR);
  147.             }      
  148.             else
  149.             {
  150.                 // Is the file UTF-8 even though it doesn't have UTF-8 BOM.
  151.                 if ((bUTF8= IsTextUTF8((LPSTR) lpBuf, len)))
  152.                 {
  153.                     ftOpenedAs= FT_UTF8;
  154.                     cpTemp= CP_UTF8;
  155.                 }
  156.                 // well, not it must be an ansi file!
  157.                 else
  158.                 {
  159.                     ftOpenedAs= FT_ANSI;
  160.                     cpTemp= CP_ACP;
  161.                 }
  162.             }
  163.             break;
  164.         }            
  165.     }
复制代码

结帖率:43% (3/7)
发表于 2010-1-4 11:10:30 | 显示全部楼层   广东省揭阳市
看不懂,顶顶
回复 支持 反对

使用道具 举报

结帖率:100% (8/8)
发表于 2010-1-4 11:17:28 | 显示全部楼层   山东省威海市
这哪是汇编啊
回复 支持 反对

使用道具 举报

结帖率:100% (2/2)
发表于 2011-7-29 23:33:47 | 显示全部楼层   浙江省温州市
我也一头雾水的出来咯
回复 支持 反对

使用道具 举报

结帖率:100% (2/2)
发表于 2011-12-24 10:26:04 | 显示全部楼层   北京市北京市
也一头雾水的{:3_227:}
回复 支持 反对

使用道具 举报

结帖率:100% (10/10)
发表于 2011-12-24 12:11:33 | 显示全部楼层   山东省烟台市
我也一头雾水的出来咯
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 致发广告者

发布主题 收藏帖子 返回列表

sitemap| 易语言源码| 易语言教程| 易语言论坛| 诚聘英才| 易语言模块| 手机版| 广告投放| 精易论坛
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表精易立场!
论坛帖子内容仅用于技术交流学习和研究的目的,严禁用于非法目的,否则造成一切后果自负!如帖子内容侵害到你的权益,请联系我们!
防范网络诈骗,远离网络犯罪 违法和不良信息举报电话0663-3422125,QQ: 800073686,邮箱:800073686@b.qq.com
Powered by Discuz! X3.4 揭阳市揭东区精易科技有限公司 ( 粤ICP备12094385号-1) 粤公网安备 44522102000125 增值电信业务经营许可证 粤B2-20192173

快速回复 返回顶部 返回列表