DelphiXE Ansi字符串UTF-8编码判断
来源:互联网 发布:投资项目经济评价软件 编辑:程序博客网 时间:2024/06/15 02:56
DelphiXE Ansi字符串UTF-8编码判断
找了半天也没找到Delphi语言直接可用的UTF-8编码判断的代码
以下代码摘抄改编于
http://bbs.csdn.net/topics/370095245
#7 用户id: xiaoc1026
回答的C++代码
IDE为DelphiXE
/// <summary>/// 判断字符串是否为 UTF-8 编码/// </summary>/// <param name="AnsiStr">输入字符串</param>/// <returns>输入是否为 UTF-8 编码</returns>function IsWordsUTF8(AnsiStr: AnsiString): Boolean;var I, iCount, chr: Integer; c: AnsiChar; nBytes: Integer; // UFT-8可用1-6个字节编码,ASCII用一个字节 bAllAscii: Boolean; // 如果全部都是ASCII, 说明不是UTF-8begin Result := False; nBytes := 0; bAllAscii := True; iCount := Length(AnsiStr); for I := 1 to iCount do begin c := AnsiStr[I]; chr := Ord(c); // 判断是否ASCII编码,如果不是,说明有可能是UTF-8,ASCII用7位编码,但用一个字节存,最高位标记为0,o0xxxxxxx;中文ASCII编码可能最高位为1 if (chr and $80) <> 0 then bAllAscii := False; // 如果不是ASCII码,应该是多字节符,计算字节数 if nBytes = 0 then begin if chr > $80 then begin if (chr>=$fc) and (chr<=$fd) then // 1111 1100 and 1111 1101 nBytes := 6 else if chr>=$f8 then // 1111 1000 nBytes := 5 else if chr>=$f0 then // 1111 0000 nBytes := 4 else if chr>=$e0 then // 1110 0000 nBytes := 3 else if chr>=$c0 then // 1100 0000 nBytes := 2 else Exit(False); Dec(nBytes); end; end else // 多字节符的非首字节,应为 10xxxxxx begin if (chr and $c0) <> $80 then Exit(False); Dec(nBytes); end; end; // 违返规则 if nBytes > 0 then Exit(False); // 如果全部都是ASCII, 说明不是 UTF-8 if bAllAscii then Exit(False); Result := True;end;
0 0
- DelphiXE Ansi字符串UTF-8编码判断
- 字符编码方式及判断整理(ANSI,Unicode,utf-8,utf-16,utf-32)
- UTF-8转化ANSI编码
- 判断字符串是否为 utf-8 编码
- Ansi编码和UTF-8编码
- 字符编码 Unicode UTF-8 UTF-16 UTF-32 ANSI
- ANSI和UTF-8文本编码
- ANSI、UNICODE、UTF-8 文件编码转换
- ASCII、ANSI、UNICODE及UTF-8编码
- UTF-8与ANSI 编码转换
- ANSI与UTF-8编码转换
- 字符编码(ANSI,UTF-8,BINCODE)
- ASCII、ANSI、Unicode及UTF-8编码
- UTF-8与ANSI 编码转换
- ASCII、ANSI、UNICODE及UTF-8编码
- ASCII、ANSI、UNICODE及UTF-8编码
- ASCII、ANSI、UNICODE及UTF-8编码
- ANSI,Unicode,UTF-8编码详解
- Android中path基本使用总结
- 搭建一个简单、易用的 Android 项目框架之 BaseAdapter(三)
- React Native之DrawerLayoutAndroid仿网易云音乐滑动菜单
- 用SHELL脚本自动化安装Nagios服务器端和客户端的
- 插入排序
- DelphiXE Ansi字符串UTF-8编码判断
- PAGE_ALIGN 详解 纯属原创 转载注明出处
- String三个注意事项
- 安卓解压zip文件,解压后多级目录显示
- Commit message 和 Change log 编写指南
- Android Dalvik虚拟机内存分配问题
- 支持向量机通俗导论(理解SVM的三层境界)
- 【上机笔试之三】快速排序
- 摘要(Hash)算法总结