delphi获取文件编码
2016-03-10 13:30
656 查看
//delphi获取文件编码 unit EncodeUnit; interface uses SysUtils, Windows, Classes; type TTextFormat = ( tfAnsi, tfUnicode, tfUnicodeBigEndian, tfUtf8 ); const TextFormatFlag: array [tfAnsi..tfUtf8] of Word = ( $0000,$FFFE,$FEFF,$EFBB ); function GetFileEncodeing(const FileName: string): TTextFormat; function WideStringToString(const WS: WideString; CodePage: Word): string; implementation //高低字节互换 function WordLoHiExchange(w: Word): Word; register; asm XCHG AL, AH end; //取得文件编码 function GetFileEncodeing(const FileName: string): TTextFormat; var W: Word; begin try with TFileStream.Create(FileName,fmOpenRead or fmShareDenyNone) do begin Seek(0, soFromBeginning); Read(W,2); W := WordLoHiExchange(W); //因为是以Word数据类型读取,故高低字节互换 if W=TextFormatFlag[tfUnicode] then Result := tfUnicode else if W=TextFormatFlag[tfUnicodeBigEndian] then Result := tfUnicodeBigEndian else if W=TextFormatFlag[tfUtf8] then Result := tfUtf8 else Result := tfAnsi; Free; end; except Result := tfAnsi; end; end; //Unicode 转 ANSI function WideStringToString(const WS: WideString; CodePage: Word): string; var InputLength, OutputLength: Integer; begin InputLength := Length(WS); OutputLength := WideCharToMultiByte(CodePage, 0, PWideChar(WS), InputLength, nil, 0, nil, nil); SetLength(Result, OutputLength); WideCharToMultiByte(CodePage, 0, PWideChar(WS), InputLength, PAnsiChar(Result), OutputLength, nil, nil); end; end.
相关文章推荐
- 如何解决delphi xe8编译大程序时内存不够的问题。
- 书_Delphi
- delphi 调试的时候变量全部显示Inaccessible value的解决办法
- Delphi:对TNotifyEvent的理解
- Delphi Socket通信及多线程编程总结
- Delphi读写二进制文件
- todo:关于Delphi的几个问题
- Delphi7 中文汉字转网址格式 Utf8编码转换(淘宝搜索中文转网址)
- TSkinData控件Version:5.8.10.1在Delphi7上的一个 BUG
- DELPHI新版本WEBSERVICE的变化
- delphi 手势
- Delphi XE8 TStyleBook的使用
- Delphi第三方组件安装DCU.PAS.DPK.BPL.ActiveX控件
- Delphi的TService的輸入桌面切換(服务程序)(windows登录界面如何截图)(使用了OpenDesktop和GetThreadDesktop等API)
- delphi使用VB的dictionary对象
- 解析Delphi 窗口置顶,及非主窗口置顶
- Delphi的windows剪切板操作函数
- delphi 路径函数
- Delphi RichEdit操作
- delphi2010泛型练习